Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda