使用关键字this时nodejs bcrypt出现问题

时间:2016-11-14 13:48:55

标签: node.js this bcrypt

我是nodejs的新手,但我正在尝试将其用作我正在设计的在线游戏的服务器。我遇到了教程,我只是在玩代码,看看我如何修改它以满足我的需要。 https://github.com/scotch-io/easy-node-authentication/tree/linking

我无法解决的一件事是,在使用bcrypt时,我的代码试图找出问题:

//check-hash

var bcrypt   = require('bcrypt-nodejs');
var salt = bcrypt.genSaltSync(8);

var newAccount = {
    username: "admin",
    password: "1235",
    email: "test@domain.com",
    AccCreateOn: Date.now(),
    LastLogin: Date.now()
}
newAccount.generateHash  = function(password) {
    return bcrypt.hashSync(password,  salt);
};

// checking if password is  valid
newAccount.validPassword  = function(password) {
    return bcrypt.compareSync(password,  this.password);
};
console.log (newAccount.password);
newAccount.password = newAccount.generateHash(this.password);
console.log(newAccount.validPassword("1235")); //false
console.log(newAccount.validPassword("1236"));//false

我已经弄清楚错误的路线是

return bcrypt.compareSync(password,  this.password);

如果我将 this.password 更改为 newAccount.password ,它会正常工作。

出了什么问题,我认为这个上下文中的.password是newAccount.password,还是不是?

0 个答案:

没有答案