我創建的模式用戶是這樣的:貓鼬虛擬現場
var schema = new Schema({
username: {
type: String,
unique: true,
required: true
},
hashedPassword: {
type: String,
required: true
},
salt: {
type: String,
required: true
}
});
schema.virtual('password')
.set(function(password) {
this._plainPassword = password;
this.salt = Math.random() + '';
this.hashedPassword = this.encryptPassword(password);
})
.get(function() { return this._plainPassword; });
schema.methods.encryptPassword = function(password) {
return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
};
然後我想用這兩種方法來更改密碼:
工作不錯
user.findById('userId ..',function(err,user){user.password ='456'; user.save(cb); })
爲什麼這種方法不起作用?
User.findByIdAndUpdate( '用戶id',{$組:{密碼: '456'}},CB)