Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
"Cast to number failed for value ... at path ... " Error when using updateOne with $pop

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The $pop operator expects a number (1 or -1) after cartItems:, see the documentation.

To remove an element based on a filter, use $pull.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!