为什么未定义哈希值?

编程入门 行业动态 更新时间:2024-10-07 08:24:28

为什么<a href=https://www.elefans.com/category/jswz/34/1771258.html style=未定义哈希值?"/>

为什么未定义哈希值?

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

更多推荐

为什么未定义哈希值?

本文发布于:2024-05-07 11:35:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756061.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未定义   哈希值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!