Todo.findById method

The last method we're going to look at is Todo.findById. Now, findById is fantastic if you are just looking for a document by its identifier. There is no way to query by anything else other than the ID and all you do is you pass in the id as the argument. You don't have to make a query object and you don't have to set an _id prompt. With this in place, we can now do the exact same thing we did with findOne. I'm going to prove that by taking the then call, pasting it inside Todo.findById, and just changing the print statement from Todo to Todo By Id:

Todo.findById(id).then((todo) => {
   console.log('Todo By Id', todo);
});

Now if I save the file, nodemon is going to rerun and we get the exact same result for both:

If you ...

Get Advanced Node.js Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.