Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

maybe

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

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda