skip to Main Content

enter image description here

I have the following error :

UnhandledPromiseRejectionWarning: 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`

2

Answers


  1. 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')
    }
    
    Login or Signup to reply.
  2. 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');
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search