While testing my api routes in postman, I got this error "Cast to ObjectId failed for value "632aa0c8930ca0decc7d042b\n" (type string) at path "_id" for model "User"".
Here is the controller and route part of the update api:
// routes
app.put("/api/users/:id", authorization, UserController.updateUser);
// controller
updateUser: async (req, res) => {
try {
const updatedUser = await User.findByIdAndUpdate(req.params.id, req.body, {new:true, runValidators:true})
res.status(200).json(updatedUser)
} catch(err) {
res.status(500).json(err)
}
}
Here is the error shown in postman:
{
"stringValue": ""632aa0c8930ca0decc7d042b\n"",
"valueType": "string",
"kind": "ObjectId",
"value": "632aa0c8930ca0decc7d042b\n",
"path": "_id",
"reason": {},
"name": "CastError",
"message": "Cast to ObjectId failed for value "632aa0c8930ca0decc7d042b\n" (type string) at path "_id" for model "User""
}
What can go wrong? Any help would be appreciated