Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

114
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

maybe

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

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda