Nodejs and mongoDB objectId failed for value

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)
            }
        })

pleaaaaaaaaase any help !!!

try to provide more context and explain what debugging you already did, it may be that with missing context it’s not easy to help

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.