hello everyone ! i got this error and i can’t solve it so please any help would be greatly appreciated!
Error: CastError: Cast to ObjectId failed for value "{ id: 'wishlist' }" (type Object) at path "_id" for model "Product"
here is the code:
const addtowishlist = asyncHandler(async(req,res)=>{
const {_id} = req.user;
const{prodId} = req.body;
try {
const user = await User.findById(_id);
const alreadyadded = user.wishlist.find((id)=>id.toString()=== prodId);
if(alreadyadded){
let user = await User.findByIdAndUpdate(
_id,
{
$pull: {wishlist: prodId},
},
{
new: true,
}
);
res.json(user);
}
else{
let user = await User.findByIdAndUpdate(
_id,
{
$push: {wishlist: prodId},
},
{
new: true,
}
);
res.json(user);
}
} catch (error) {
throw new Error(error)
}
})