【发布时间】:2015-06-10 03:11:45
【问题描述】:
我为这样的用户创建了一个架构:
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) { 用户密码 = '456'; 用户.保存(cb); })
-
为什么这种方法不起作用?
User.findByIdAndUpdate('userId', {$set: {password: '456'}}, cb)
【问题讨论】: