Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

240
Vistas
"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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda