Getting error while using " return res.json({ }) " #6172
Replies: 8 comments
-
the problem was in @types/express@5.0.0 , when i used previous version the issue was solved |
Beta Was this translation helpful? Give feedback.
-
EDIT: THIS COMMENT IRRELEVANT Looking to lend a hand, what help do you want here? More info on the bug, updated types for 5.0, overload that allows res return? |
Beta Was this translation helpful? Give feedback.
-
Q1: is it express intention to not call next and return out of a middleware? Doco is only representative of calling next(), to handle errors out of them it's handed to custom error handler EDIT: BELOW IRRELEVANT Note1: I have looked at the types that are throwing in types/express-serve-static-core, they seem to have no changes since v4. This was a quick look. Will get to more work over the weekend |
Beta Was this translation helpful? Give feedback.
-
It seems that types for 5 are working as intended because next is the intention of middleware instead of returning directly out of them. |
Beta Was this translation helpful? Give feedback.
-
Related issue #5987 |
Beta Was this translation helpful? Give feedback.
-
Hey @tanmayvaij hope you find well, so basically something that you can do with that version of @types/express@5.0.0 is changing a little bit your approach instead of using
|
Beta Was this translation helpful? Give feedback.
-
This issue is related to this interface in express-serve-static-core
adding the |
Beta Was this translation helpful? Give feedback.
-
The error you're encountering occurs because the tokenVerifier middleware returns a response in case of an error (res.json(...)). The RequestHandler type from Express expects the middleware to either return void or Promise. Since tokenVerifier is returning Response | undefined, TypeScript throws this error. Here’s how to address it: Ensure that tokenVerifier always calls next() or returns when it’s done, without returning any response directly. Here's an updated version of tokenVerifier:
Changes Explained The return type for tokenVerifier is set to void, making sure it either calls next() or returns a response without breaking Express's expected type. If you still encounter issues, make sure that your route handler (verifyUserController) is also typed correctly and doesn’t directly return a response. |
Beta Was this translation helpful? Give feedback.
-
using "express": "^4.21.0",
created this middleware for token verification
i get this err while starting the server
Beta Was this translation helpful? Give feedback.
All reactions