首页 > 解决方案 > 对象:空原型不允许检索值

问题描述

尝试比较密码时出现此错误

UnhandledPromiseRejectionWarning:错误:需要数据和哈希参数

事情是 user.password 返回未定义,但是当我执行 console.log(user) 它返回模型

像这样

ModelBase {
  attributes:
   [Object: null prototype] {
     username: 'billy',
     id: 4,
     password:
      '$2b$12$oIIn*******AnNkr/Pt89S****W3Vi2o8DYBgnEy9t9gcje',
     email: 'exampleman@example.com',
     created_at: 2019-05-28T20:00:37.164Z,
     updated_at: 2019-05-28T20:00:37.164Z },

所以我不明白为什么我在做的时候不确定user.password

护照.js

 .......
    passport.use('login', 
        new Local(
        {
            usernameField:'username',
            passwordField:'password',
            session:false
        },
        (username, password, done, req) => {
            try{
                User.forge({username: username}).fetch()
                .then(user => {
                    if(user === null){
                        return done(null, false, {message: 'Username doesn\'t exist'})
                    }
                    else{

                        console.log(user);  // logs out user model along with password info 

                       // not getting user.password from user model.
                      bcrypt.compare(password, user.password)
                        .then(response => {
                            if(response !== true){
                                console.log('passwords do not match');
                                return done(null, false, {message:'passwords do not match'} )
                            }
                            console.log('user found & authenticated');
                            return done(null, user); 
                        })                
                    }
                })
            }catch(err){
                done(err);
            }
        }
    ))

标签: javascriptnode.jsexpresspassport.jsbcrypt

解决方案


尝试与 比较user.attributes.password


推荐阅读