Create a User model:
user/models.js
'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// define the userSchema
var userSchema = new Schema({
name : {
givenName : String,
familyName : String
}
});
// Export the User model
exports.User = mongoose.model('User', userSchema);
For more information on mongoose please read mongoose documentation
Check the test runner.
You should now see that our test passed.
addition
✓ should add 1+1 correctly
Users: models
#create()
✓ should create a new User
✔ 2 tests complete (21 ms)
First modify the test:
describe('#create()', function () {
it('should create a new User', function (done) {
// Create a User object to pass to User.create()
var u = {
name: {
givenName: 'Barack',
familyName: 'Obama'
},
// new attributes
emails: [
{
type: 'home',
value: 'home@example.com'
},
{
type: 'work',
value: 'work@example.com'
}
]
};
User.create(u, function (err, createdUser) {
// Confirm that that an error does not exist
should.not.exist(err);
// verify that the returned user is what we expect
createdUser.name.givenName.should.equal('Barack');
createdUser.name.familyName.should.equal('Obama');
// new tests
createdUser.emails[0].type.should.equal('home');
createdUser.emails[0].value.should.equal('home@example.com');
createdUser.emails[1].type.should.equal('work');
createdUser.emails[1].value.should.equal('work@example.com');
// Call done to tell mocha that we are done with this test
done();
});
});
});
You should see that the test are now failing.
addition
✓ should add 1+1 correctly
Users: models
#create()
1) should create a new User
✖ 1 of 2 tests failed:
1) Users: models #create() should create a new User:
TypeError: Cannot read property '0' of undefined
Add an emails list attribute to the User schema:
// ...
// define the emailSchema
var emailSchema = new Schema({
// Since `type` is special keyword in mongoose we must set the def. to
// and object. I.e. this would not work:
// type: String,
type : {type: String},
value : String
});
// define the userSchema
var userSchema = new Schema({
name : {
givenName : String,
familyName : String
},
emails: [emailSchema]
});
// ...
Test should now be passing. If they are not try restarting the test runner. command+c
and running npm start