Advanced Node.js Development
上QQ阅读APP看书,第一时间看更新

Setting up the email property

The first thing I'm going to do is make a variable to store this new model, a variable called User, and I'm going to set that equal to mongoose.model, which is how we can make our new User model. The first argument, as you know, needs to be the string model name. I'm going to use the exact same name as I specified over in the variable, although it could be different. I just like to keep things using this pattern, where the variable equals the model name. Next up, as the second argument, we can specify the object where we configure all the properties a User should have.

var User = mongoose.model('User', {

});

Now as I mentioned previously, we'll be adding others later, but for now, adding support for an email property will be good enough. There's a few things I want to do on this email. First up, I want to set the type. An email is always going to be a string, so we can set that type equal to String.

var User = mongoose.model('User', {
  email: {
    type: String,

}
});

Next up, we're going to require it. You can't make a user without an email, so I'll set required equal to true. After required, we're going to go ahead and trim that email. If someone adds spaces before or after it, it's clearly a mistake, so we'll go ahead and remove those for the User model, making our application just a little more user-friendly. Last but not least, what we want to do is set up a minlength validator. We'll be setting up custom validation later, but for now minlength of 1 is going to get the trick done.

var User = mongoose.model('User', {
  email: {
    type: String,
    required: true,
    trim: true,
    minlength: 1
  }
});

Now, I am going to go ahead and create a new instance of this User and save it. Before I run the script though, I will be commenting out our new Todo. Now, we can make a new instance of this User model. I'm going to make a variable called user and set it equal to new User, passing in any values we want to set on that user.

var User = mongoose.model('User', {
  email: {
    type: String,
    required: true,
    trim: true,
    minlength: 1
  }
});
 
var user = new User({

});

I'm going to run it with nothing at first, just to make sure the validation is working. Next to the user variable, I can now call user.save. The save method returns a promise, so I can tack on a then callback. I'm going to add a success case for this one, and an error handler. The error handler will get that error argument, and the success case will get the doc. If things go well, I'll print a message using console.log('User saved', doc), followed by the doc argument. No need to format it for this example. I'll do the same thing for the error handler, using console.log('Unable to save user') followed by the error object:

var user = new User({

});

user.save().then((doc) => {
  console.log('User saved', doc);
}, (e) => {
  console.log('Unable to save user', e);
});

Since we're creating a user with no properties, we would expect the error to print. I'm going to save server.js and restart the file:

We get our error. It's a validation error called Path 'email' is required. Mongoose is letting us know that we do indeed have an error. The email does need to exist, since we set required equal to true. I'm going to go ahead and put a value, setting email to my email, andrew@example.com, and I'll put a few spaces afterwards:

var user = new User({
email: 'andrew@example.com '
});

This time around, things should go as expected and trim should be trimming the end of that email, removing all of the spaces, and that's exactly what we get:

The User was indeed saved, which is great, and the email has been properly formatted. Now obviously, I could have put a string in like 123, and it would have worked because we don't have custom validation set up just yet, but we have a pretty good starting point. We have the User model, and we have our email property set up and ready to go.

With this in place, we are now going to start creating the API. In the next section, you're going to install a tool called Postman, which is going to help us test our HTTP requests, and then we're going to create our very first route for our Todo REST API.