Home > database >  Mongo findById() only works sometimes even when passed a valid ID
Mongo findById() only works sometimes even when passed a valid ID

Time:04-19

I am having a strange issue querying a Mongo DB collection. I am using findById() to get a single item which 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 if the id passed is correct and the document exists with the exact id.

If anyone has any ideas 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.

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

CodePudding user response:

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

CodePudding user response:

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.

  • Related