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

88
Vistas
Getting error in mongodb cast issue for valid object id

My code was working fine but after taking fresh code I am getting this error but as I can see my id is valid Object Id

Cast to ObjectId failed for value "5848e9ecaec0f31372816a26" at path "_id" for model "user"

how to fix this?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You have to cast the data to mongodb object id. If you are using mongoose, you can actually do these below.

const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;

[your_mongodb_model/collection].query({ _id: ObjectId(id) });

or simply, you can do these,

[your_mongodb_model].query({ _id: mongoose.Types.ObjectId(id) });
over 4 years ago · Santiago Trujillo Denunciar

0

The reason for this error is documented in this mongoose issue. A workaround is to downgrade to mongoose version <= 4.7.2 or use Node version >=6.

over 4 years ago · Santiago Trujillo 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