最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

为什么未定义哈希值?

网站源码admin22浏览0评论

为什么未定义哈希值?

为什么未定义哈希值?

盐值已正确记录,但哈希值未定义。我无法弄清楚这里有什么不对。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是否未返回undefinednull

编辑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);                       
           });        
        })              
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论