未定义哈希值?"/>
为什么未定义哈希值?
盐值已正确记录,但哈希值未定义。我无法弄清楚这里有什么不对。users.js是一个模型,其他代码是一个控制器。users.js
UserSchema.statics.updateOTPOnDatabase = function(mobile,otp){
bcrypt.genSalt(10, function(err, salt){
console.log(salt); //this works fine
bcrypt.hash(otp, salt, function(err, hash){
console.log(hash); //giving undefined
});
})
}
login.js
exports.login = (req, res) => {
const mobile = _.pick(req.body, ['mobile_number']);
User.findByMobile(mobile).then((user) => {
const otp = Math.round(Math.random()*9000 + 1000);
User.updateOTPOnDatabase(user.mobile_number, otp).then(res => {
console.log(res);
}).catch(err => {
var response = {
status: 'failure',
message: err.message
};
res.send(response);
});
}).catch(err => {
var response = {
status: 'failure',
message: err.message
}
res.send(response);
});
};
回答如下:我唯一看到的问题是您的updateOTPOnDatabase
函数,因为在此函数中您正在接受1个参数otp
,但是当您调用此函数时,您正在传递2个参数user.mobile_number,otp
这是您的定义
UserSchema.statics.updateOTPOnDatabase = function(otp){ }
这是您调用它的方式
User.updateOTPOnDatabase(user.mobile_number, otp) { }
因此,您可能需要修复1或另一个,还请检查user.mobile_number
是否未返回undefined
或null
编辑2
根据注释和每个Github code,需要一个字符串,因此您可以使用otp
方法将.toString()
转换为字符串
UserSchema.statics.updateOTPOnDatabase = function(mobile,otp){
bcrypt.genSalt(10, function(err, salt){
console.log(salt);
bcrypt.hash(otp.toString(), salt, function(err, hash){
console.log(hash);
});
})
}
更多推荐
为什么未定义哈希值?
发布评论