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

359
Visualizações
Mongo findById() only works sometimes even when passed a valid ID

I am having a strange issue querying a Mongo DB collection. I am using findById() to get a single item that works sometimes and not others.

I have checked the id being passed to the server route and in all cases, they match perfectly with the targeted document in the collection.

Here is the basic code:

router.get("/:postId", async (req, res) => {
  console.log('id : ', req.params.postId)
  console.log('type: ', typeof(req.params.postId)) // id is a string
  try {
    const post = await Post.findById(req.params.postId).exec();
    console.log('post :', post) // sometimes null
    res.json(post);
  } catch (err) {
    res.json({ message: err });
  }
});

In the above route, only certain posts will be found while others come back null. This happens regardless of whether the id passed is correct and the document exists with the exact id.

If anyone has any ideas about what could be going wrong here I'd much appreciate the help!

EDIT

I have done some more debugging and think it is something to do with the Schema for the Post model.

For example, this object will be found:

{
"tags": ["foo"],
"_id": "8394839483fhg020834903",
"title": "bar",
"content": "baz",
"isPrivate": true,
}

But this one will not because of the missing isPrivate property.

{
"tags": [],
"_id": "5e0fdc631ef5c46b285a4734",
"title": "New post",
"content": "Some content here",
}

I have tested this across multiple queries and it appears to the root of the problem.

I have tried adding

isPrivate: {
    required: false
  }

To the Schema but it doesn't seem to solve the issue.

Here is the full Schema

const postSchema = mongoose.Schema({
  title: {
    type: String,
    required: true
  },
  content: {
    type: String,
    required: true
  },
  tags: [{ type: String }],
  date: {
    type: Date,
    default: Date.now
  },
  isPrivate: {
    type: Boolean
    required: false
  }
});

I'm not a Mongo/Mongoose expert, so any guidance would be much appreciated.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If post id match with any record it return data, otherwise it will return null. You should handle the exception

router.get("/:postId", async (req, res) => {
   try {
     const post = await Post.findById(req.params.postId).exec();
     if(post) {
       return res.json(post);
     }
     res.json({ message:'No Post found' });
   } catch (err) {
     res.json({ message: err });
   }
});

You can manually check is record exists against a post id. You can use MongoDB Compass for gui browse the record

about 4 years ago · Juan Pablo Isaza Relatório

0

I believe the issue might be with your _id as per mongo standard _id should be a String is of 12 bytes or a string of 24 hex characters.

We can check if the _id is valid using mongoose.isValidObjectId()

I did run this check on your objects that you posted and indeed 1 is invalid while other is valid

const mongoose = require('mongoose');

console.log(`is '8394839483fhg020834903' valid - ${mongoose.isValidObjectId('8394839483fhg020834903')}`);
console.log(`is '5e0fdc631ef5c46b285a4734' valid - ${mongoose.isValidObjectId('5e0fdc631ef5c46b285a4734')}`);

It gives meValid/Invalid

You will have to check what is modifying your ID's in the code, you can upload your schema to get a better understanding as well.

about 4 years ago · Juan Pablo Isaza 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