Home > Back-end >  POST request... UnhandledPromiseRejectionWarning: Unhandled promise rejection
POST request... UnhandledPromiseRejectionWarning: Unhandled promise rejection

Time:10-14

When making a post request I get this error:

UnhandledPromiseRejectionWarning: Unhandled promise rejection.

If somebody could explain why this is happening I would appreciate it :) Thanks

router.post("/", async (req, res) => {
 

  let client = new Client(req.body);

  try {
    savedClient = await client.save();
    res.location(`/${savedClient._id}`).status(201).json(savedClient);
  } catch {
    res.status(500).json(savedClient.error);
  }
});

CodePudding user response:

maybe

let savedClient = await client.save();

CodePudding user response:

I think it should be

...
} catch (error) {
    res.status(500).json(savedClient.error);
}

because the promise's rejection error should be catch by the try/catch statement.

  • Related