我试图用mongoose在mongodb中保存一个新文档,但我得到了ValidationError: Path 'email' is required., Path 'passwordHash' is required., Path 'username' is required.
个,尽管我提供了邮箱、密码哈希和用户名.
这是用户模式.
var userSchema = new schema({
_id: Number,
username: { type: String, required: true, unique: true },
passwordHash: { type: String, required: true },
email: { type: String, required: true },
admin: Boolean,
createdAt: Date,
updatedAt: Date,
accountType: String
});
这就是我创建和保存用户对象的方式.
var newUser = new user({
/* We will set the username, email and password field to null because they will be set later. */
username: null,
passwordHash: null,
email: null,
admin: false
}, { _id: false });
/* Save the new user. */
newUser.save(function(err) {
if(err) {
console.log("Can't create new user: %s", err);
} else {
/* We succesfully saved the new user, so let's send back the user id. */
}
});
那么为什么mongoose会返回一个验证错误,我不能使用null
作为临时值吗?