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

414
Vistas
Why I get am CastError: Cast to ObjectId failed?

I get the following when I call a route in my project:

serveur running PORT: 7000

/user/new
CastError: Cast to ObjectId failed for value "favicon.ico" (type string) at path "_id" for model "User"
    at model.Query.exec (C:\Users\Admin\Downloads\books\node_modules\mongoose\lib\query.js:4650:21)
    at model.Query.Query.then (C:\Users\Admin\Downloads\books\node_modules\mongoose\lib\query.js:4749:15)
    at exports.findOneUser (C:\Users\Admin\Downloads\books\routes\homeController.js:38:6)    
    at Layer.handle [as handle_request] (C:\Users\Admin\Downloads\books\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Admin\Downloads\books\node_modules\express\lib\router\route.js:137:13) 
    at Route.dispatch (C:\Users\Admin\Downloads\books\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Admin\Downloads\books\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Admin\Downloads\books\node_modules\express\lib\router\index.js:281:22        
    at param (C:\Users\Admin\Downloads\books\node_modules\express\lib\router\index.js:360:14)    at param (C:\Users\Admin\Downloads\books\node_modules\express\lib\router\index.js:371:14) {
  messageFormat: undefined,
  stringValue: '"favicon.ico"',
  kind: 'ObjectId',
  value: 'favicon.ico',
  path: '_id',
  reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters
      at new BSONTypeError (C:\Users\Admin\Downloads\books\node_modules\bson\lib\error.js:41:28)
      at new ObjectId (C:\Users\Admin\Downloads\books\node_modules\bson\lib\objectid.js:65:23)
      at castObjectId (C:\Users\Admin\Downloads\books\node_modules\mongoose\lib\cast\objectid.js:24:12)
      at ObjectId.cast (C:\Users\Admin\Downloads\books\node_modules\mongoose\lib\schema\objectid.js:247:12)
      at ObjectId.SchemaType.applySetters (C:\Users\Admin\Downloads\books\node_modules\mongoose\lib\schematype.js:1180:12)
      at ObjectId.SchemaType._castForQuery (C:\Users\Admin\Downloads\books\node_modules\mongoose\lib\schematype.js:1614:15)
      at ObjectId.SchemaType.castForQuery (C:\Users\Admin\Downloads\books\node_modules\mongoose\lib\schematype.js:1604:15)

I have a route (like route.get("/", myCallBack)) which returns all the objects in my mongodb database. I cannot find where is my error.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The issue is that it's not a valid _id string. It has to be either a 12-byte binary string or a 24 hex byte string. "favicon.ico" is neither.

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