when I using the findById method defined by mongoose , I got a Cast error something like this
CastError: Cast to ObjectId failed for value "611411089cb0c839083962ba4" (type string) at path "_id" for model "Product"
at model.Query.exec (/home/user/web/mern/emartwell/node_modules/mongoose/lib/query.js:4498:21)
at model.Query.Query.then (/home/user/web/mern/emartwell/node_modules/mongoose/lib/query.js:4592:15)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
But I need the product not found error.
the route I specified is
http://localhost:5000/api/products/611411089cb0c839083962ba4
Code
productRoute.js
import express from "express";
import asyncHandler from "express-async-handler";
import Product from "../models/productModel.js";
const router = express.Router();
router.get(
"/:id",
asyncHandler(async (req, res) =>
{
const product = await Product.findById(req.params.id);
if (product)
{
res.json(product);
} else
{
res.status(404).send({ message: "Product not found" });
}
})
);
export default router;
Please help me to get rid of this problem. Thanks in advance :D