Now, the next thing I want to talk about is what happens when the ID isn't correct, and this is going to be the case because, remember, our API is going to be getting this ID from the user, which means that if the ID isn't correct we don't want our code to fail, we want to elegantly handle these errors. To prove this I'm going to go ahead and tweak the ID a little bit. IDs do have specific protocols so what I want you to do, for this example, is find a number in your ID. I'm going to go with the first character because it happens to be a number, and just increment it by one. I'm going to go from 5 to 6. Now we have a valid ID but the ID is not going to be in the database because I tweaked it, and obviously the other Todo in the database does not match this ID.
Now, with this in place, you can see as we restart the server we get an empty array for the find call, and we get null for both findOne and findById:

When your ID does not match anything in the database, an error is not going to get thrown; it's still going to fire the success case, it's just going to fire it either with an empty array or with null, which means when we want to handle that case where the ID doesn't exist in the database, all we have to do is add an if statement. Inside the Todo.findById statement, I can add an if statement. If there is no todo, we're going to do something, and that something is going to be to use return which prevents the rest of the function from executing, and we'll print a little message, console.log('Id not found'):
Todo.findById(id).then((todo) => {
if(!todo) {
return console.log('Id not found');
}
console.log('Todo By Id', todo);
});
Now if I save the file the last call should look a little different:

As shown in the preceding screenshot, instead of getting Todo with null, we get Id not found, and this is perfect. Now we know how to query using findOne and findById, and we also know how to handle situations where the ID you're querying for doesn't actually exist inside of the collection. I'm going to set the ID back to its original value, changing 6 to 5, and if I save the file, nodemon's going to restart and we're going to get our document back.