- Advanced Node.js Development
- Andrew Mead
- 159字
- 2021-08-27 19:06:00
Creating a brand-new Todo
I'm going to make a variable called newTodo, although you could call it anything you like; the name here is not important. What is important though is that you run the Todo function. This is what comes back from mongoose.model as a constructor function. We want to add the new keyword in front of it because we're creating a new instance of Todo.
Now, the Todo constructor function does take an argument. It's going to be an object where we can specify some of these properties. Maybe we know that we want text to equal something like Cook dinner. Right in the function, we can specify that. text equals a string, Cook dinner:
var newTodo = new Todo({ text: 'Cook dinner' });
We haven't required any of our attributes, so we could just stop here. We have a text property; that's good enough. Let's go ahead and explore how to save this to the database.