Creating an instance of Mongoose model

Inside server.js, let's make a variable called todo to do what we've done previously, creating an instance of a Mongoose model. We're going to set it equal to new Todo, passing in our object and passing in the values we want to set. In this case, we just want to set text. We're going to set text to req.body, which is the object we have, and then we're going to access the text property, like so:

app.post('/todos', (req, res) => {
  var todo = new Todo({
    text: req.body.text
  });

Next up, we're going to call todo.save. This is going to actually save the model to the database, and we're going to be providing a callback for a success case and an error case.

app.post('/todos', (req, res) => { var todo = new Todo({ ...

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.