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

404
Views
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 answers
Answer question

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 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!