2014-11-26 4 views
4

나는 이와 같은 정보를 업데이트하고 싶은 사용자 스키마가 있습니다.몽구스 모델 (node.js)의 한 필드 업데이트

User.findOne({_id: idd}, function(err, usr){ 
    usr.info = "some new info"; 
    usr.save(function(err) { 
    }); 
}); 

그러나 모델이 나는 그것이 allready hased 암호를 취 저장하려고 다시 해시 때 이제 암호

UserSchema.pre('save', function(next) { 
    if (this.password && this.password.length > 6) { 
     this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64'); 
     this.password = this.hashPassword(this.password); 
    } 

    next(); 
}); 

을 해시 저장에 후크를 가지고, 어떤 아이디어는 어떻게이 문제를 방지하려면?

답변

7

Model.Update을 사용하고 새 암호 만들기를 독립 기능으로 이동하십시오.

var salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');; 
var newPassword = this.hashPassword("someNew password"); 
User.update({_id: idd}, { 
    info: "some new info", 
    password: newPassword 
}, function(err, affected, resp) { 
    console.log(resp); 
}) 
+0

감사합니다. 해결책이있는 것 같습니다. :) – user1930848

3

isModified을 사용해 보셨습니까?

UserSchema.pre('save', function(next) { 
    if (this.password && this.password.length > 6 && MYMODEL.isModified('password')) { 
     this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64'); 
     this.password = this.hashPassword(this.password); 
    } 

    next(); 
});