1
我有一个小项目来向我介绍一些前端技术。我正在使用Node,Express,Pug和MongoDB。猫鼬不保存正确的模式
我在user.js的文件中定义的用户模式:
var userSchema = mongoose.Schema({
username : String,
password : String,
jobs : [{ type: mongoose.Schema.Types.Mixed }]
});
然后,在我的passport.js文件,我开始注册过程。
User.findOne({ 'username' : username }, function(err, user) {
// if there are any errors, return the error
if (err) {
console.log(err);
return done(err);
}
// check to see if theres already a user with that email
if (user) {
console.log('user exists');
return done(null, false, req.flash('signupMessage', 'That username is already taken.'));
} else {
console.log('creating new user...');
// if there is no user with that email
// create the user
var newUser = new User();
newUser.username = username;
newUser.password = newUser.generateHash(password);
newUser.jobs = [{ website: 'google.com' }];
// save the user
newUser.save(function(err) {
if (err) {
console.log(err);
throw err;
}
console.log('user saved: ', newUser);
return done(null, newUser);
});
}
});
的成功后保存新的用户为:
{
"_id": {
"$oid": "5967d2acc64d953330a3ac32"
},
"__v": 0
}
我的目标是有在网站的链接,可以推入阵列该用户的数据库的数组。
感谢您的任何援助。
我修改了作业数组的类型并修改了newUser的创建。现在我发现User.generateHash既不是一个既是实例又是静态函数的函数。 – klewis