Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
POST request... UnhandledPromiseRejectionWarning: Unhandled promise rejection

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

UPDATE

So, the this code POSTs successfully. But, when I uncomment the validation code I get that same error...

router.post("/", async (req, res) => {
  //let client = validate(req.body);

  //if (client.error) {
    //res.status(400).json(result.error);
    //return;
  //}

  let client = new Client(req.body);

  try {
    let savedClient = await client.save();
    res.location(`/${savedClient._id}`).status(201).json(savedClient);
  } catch (error) {
    res.status(500).json(savedClient.error);
  }
});
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I don't see where you initialise savedClient and I think your error lies in your catch. You're referencing an object (savedClient) which doesn't appear to be in scope.

Try this:

router.post("/", async (req, res) => { 
  let client = new Client(req.body);
  try {
    let savedClient = await client.save();
    res.location(`/${savedClient._id}`).status(201).json(savedClient);
  } catch (error) {
    console.log(error)
    res.status(500).json(error);
  }
});
about 4 years ago · Juan Pablo Isaza Report

0

maybe

let savedClient = await client.save();
about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!