Open iamAravindks opened 3 years ago
Your problem is entirely unrelated to this Github repo.
An ObjectId must be exactly 24 hex characters (or 12 bytes, in binary), but your string 611411089cb0c839083962ba4
has 25 characters.
https://stackoverflow.com/questions/14940660/whats-mongoose-error-cast-to-objectid-failed-for-value-xxx-at-path-id
You could also implement a custom error handler that transforms the error to your desired response. http://expressjs.com/en/guide/error-handling.html#writing-error-handlers Could go approximately like this, haven't tested it:
app.use(function (err, req, res, next) {
console.error(err.stack)
if (err.message && err.message.includes('Cast to ObjectId failed')) {
res.status(404).send({ message: "Product not found" });
} else {
res.status(500).send('Something broke!');
}
})
when I using the
findById
method defined by mongoose, I got a Cast error something like thisBut I need the product not found error.
Code
productRoute.js
But using the below code gives the exact output I want
Please help me to get rid of this problem. Thanks in advance :D