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.