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

828
Views
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
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!