User Object Model - pcimino/nodejs-restify-mongodb GitHub Wiki
User Object Models
user.js
Schema
The top of the file is fairly straightforward. The UserSchema consists of a JSON object with variables. In Mongoose, id
is an alias for the Mongo _id
field.
virtual Assigning virtual tells Mongoose that the 'password' field is not actually used for storage, and when the setter is called, a hashed password is created.
Validation Attributes in the User object can be validated. In this case 4 of the fields are required, unless another authentication scheme is being used.
The pre('save', ...)
is used to validate for a password, if required.
Methods Currently two helper methods on the User object to manage the password and verify the password is correct.
Note: In the code you'll see I'm having some issues using the /api/user/:id
URI format. When it's working the client simply gets a 404 response, which was not really a problem. But then I started getting a 405 on all requests. So until I figure that out, I'm manually checking for request parameters and calling the appropriate GET.
Next: User List Object Models
Return Home