Saving the instance to the database

Creating a new instance alone does not actually update the MongoDB database. What we need to do is call a method on newTodo. This is going to be newTodo.save. The newTodo.save method is going to be responsible for actually saving text to the MongoDB database. Now, save returning a promise, which means we can tack on a then call and add a few callbacks.

newTodo.save().then((doc) => {

}, (e) => {

});

We'll add the callbacks for when the data either gets saved or when an error occurs because it can't save for some reason. Maybe the connection failed, or maybe the model is not valid. Either way, for now we'll just print a little string, console.log(Unable to save todo). Up above, in the success callback, we're ...

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.