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

832
Vistas
Error handler not working properly. Getting empty array instead to error in Express,Node,Mongoose API application

I am working on an API application where I am using Express, Node, and Mongoose. When I am trying findById(req.params.id)my error handler works as expected.

   exports.getProductByCustId = async(req, res, next) => {
  try {
  const products = await Product.findById(req.params.id);
  if (!products) {
    // When ID is correct but not available in database
    return next(`Products not found with id of ${id}`, 404);
  }
  res.status(200).json(products);
  catch(err) {
   next(err);
  }
};

But when I do like this, it works:

  exports.getProductByCustId = async(req, res, next) => {
  const id = req.params.id;
  let cutomerId = { cust_id: id };
  const products = await Product.find(customerId, {_id: 0, __v: 0, cust_id: 0});
  console.log(products);
  if (!products) {
    // When ID is correct but not available in database
    return next(new ErrorResponse(`Products not found with id of ${id}`, 404));
  }
  res.status(200).json(products);
};

My error handler not working and getting "[]" every time if customerId is incorrect or

I will be really thankful if anyone can help. I already tried all the possible slack channel but no luck so far. I will thankful if anyone can help.

over 4 years ago · Santiago Trujillo
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