enter image description hereUnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict`
CodePudding user response:
Firstly, declare new object doesn't need await
, so change your declare new user to const user = new User(your_object_here)
And what you mean in catch() then you throw error and catch again. Change you code to this and try again
try{
// logic here
res.send('ok')
}catch(error) {
console.log('that did not go well');
res.status(500).send('fail')
}
CodePudding user response:
This error happens because you are trying to throw an error inside a catch
bloc. (documentation)
If you really need to throw an error you must put another try
/ catch
inside your catch
block. But you should consider removing the throw
and only print the error inside the catch and send a 500
error .
try {
await user.save()
res.send("ok");
} catch (Error e) {
console.log(e);
res.status(500).send('ko');
}