I'm having this error and I'm not sure if this is an error in my query or how I set up the schema for this document. I can insert without problem though, to this document in the db. Now I'm trying to remove an element in cartItems field but can't do so.
Here is the error:
CastError: Cast to number failed for value "613997c799907eadff60cff1" (type string) at path "cartItems"
I set up my schema like this. And as much as possible I'd like it to stay that way because all of my other codes might get affected and I don't want that at the moment.
const userSchema = new mongoose.Schema({
cartItems:{
type: Array
}})
Then here is the code that updates the db:
module.exports.removeFromCart = (userId,productId)=>{
return User.updateOne({_id: userId}, {$pop: {cartItems:{item:productId}}}).then((result)=>{
console.log(result);
}).catch(error=>{
//THIS THROWS THE ERROR I"M GETTING
console.log(error);
})}
What could I be doing wrong here? By the way, I can console.log all the data being passed to the query so I'm sure I'm not passing undefined data to mongoose query.
The $pop operator expects a number (1 or -1) after cartItems:, see the documentation.
To remove an element based on a filter, use $pull.