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

575
Visualizações
Cast to ObjectId failed for value "" at path "_id" for model "Coach"

My app works locally, but in production I can't seem to fetch a specific thing from mongo using mongoose. I've tried:

var Coach = mongoose.model('Coach', CoachSchema);

  • Coach.findOne({ _id: req.session.userId}, function (err, coach) {})
  • Coach.find({ _id:req.session.userId}, function (err, coach) {})
  • Coach.findById(req.session.userId).exec(function(err, coach)){})

I've received the same error: Cast to ObjectId failed for value "586bc06bd09d870012baa5b8" at path "_id" for model "Coach"

Like I said, it works locally with both nodemon and heroku local but on my heroku production server it doesn't work.

I've read a little about this type of cast error here:

  • What's Mongoose error Cast to ObjectId failed for value XXX at path "_id"?
  • Mongoose refuses to cast valid string to ObjectId
  • Cast to ObjectId failed for value "586cc8b3ea780c071bbe2469" at path "_id" for model "User"

But no luck yet. Any help would be great. Thank you in advance.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I had faced this same issue sometimes back, for mongoose version > 4.7.2

I solved it by installing mongoose version 4.7.2

npm install mongoose@4.7.2

also, you can change your package.json so that it uses this exact version of mongoose.

"mongoose" : "4.7.2"

This bug is caused because of bson package. which gives the error

Hex is not a function on ObjectId creation

Read more about it in this link.

This bug started when mongoose was updated from version 4.7.2 to 4.7.3. You can update to newer version of mongoose when this bug is solved. You can track the bug here and here.

So, Finally you can solve this error by installing mongoose version 4.7.2

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