Next up, we're going to make that query Todo.findById. Here we're going to pass in the ID, which we have in the id variable, and then we're going to attach our success and error handlers, .then, passing in our success callback. This is going to get called potentially with the individual Todo document, and I am going to call catch as well, getting the error. We can do the error handler first. If there is an error, we're going to keep things really simple, res.status, setting it equal to 400, then we're going to go ahead and call send, leaving out the error object intentionally:
Todo.findById(id).then((todo) => {
}).catch((e) => {
res.status(400).send();
});
With this in place the only thing left to do is fill out the success handler. The first thing we need to do is make sure that a Todo is actually found. This query, if successful, might not always result in an actual document being returned. I'm going to use an if statement to check if there is no Todo. If there is no Todo, we want to respond with a 404 response code, just like we did before. We're going to return to stop the function execution, res.status. The status here will be 404 and we will be using send to respond with no data:
Todo.findById(id).then((todo) => {
if(!todo) {
return res.status(404).send();
}
}).catch((e) => {
res.status(400).send();
});