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

161
Views
mongoose findById would not work with correct Id

I was creating a database storing user information. Previously, the system worked well, after I performed npm update of mongoose, the system failed and I dont know why. The following is the error message I encountered.

CastError: Cast to ObjectId failed for value "xxxxxxxxxxxxxxx" at path "_id" for model "User"

Then, I write some simple testing code to test if the database is connected successfully. For example:

User.findOne({firstName: "David"}, function(err, user){console.log(user.lastName)});

The above worked perfectly.

I then found the corresponding Id from the database directly and create variable like:

var testId = "abcde"

and tried the following code:

User.findById(testId, function(err, user){console.log(user.firstName)});

Then it just output the error messages.

I have tried mongoose.Types.ObjectId.isValid('abcde') (abcde is just example) and it returned true. Therefore, I really have no ideas why it did not work...is it a mongoose bug?

Please help if you know the answer, thank you very much!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I just solved the problem.

As I mentioned, the findById function did not work after I updated mongoose. I was struggling on the code for few days and when I woke up this morning, I suddenly thought of upgrading my node.js.

That worked.

After upgrading node.js from v4.x.x to latest version, everything worked fine : )

This taught me a lesson that I should not upgrade a single module alone and care of compatibility issue.

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