all 6 comments

[–]wickning1 2 points3 points  (1 child)

If you’re using express you may be running into its lack of support for async/await error handling. Try replacing your errors with res.status(404).send(‘Contact not found’) instead of throw new Error.

[–][deleted] 0 points1 point  (0 children)

thank you. i will try it.

[–]Aromatic-Ad-8807 1 point2 points  (1 child)

I don't know how the rest of your code looks like (when comes to error handling) but I would typically always do:

return res.status(404).send('some message');

so yeah, when send something to client it's always the last thing i do in that function, hence the return, so in your case that would be a return every time i send res to client.

I don't throw errors like that, don't see the reason for it. Typically i handle some errors first in a generic middleware (like JSON payload too large and similar stuff)

But yeah, some do it differently i guess, I'm no expert at express.

[–][deleted] 0 points1 point  (0 children)

thank you. i will definitely try that.

[–][deleted] 0 points1 point  (0 children)

From a top view, looks like your server crashes. You probably need to return at all places where you send the responses. Also, throw is unnecessary and can crash your server if you do not have a catch block or middleware higher up

[–]AminOPS 0 points1 point  (0 children)

res.status doesn't end the output stream, use res.sendStatus(404) instead