对象:null原型不允许检索值

时间:2019-05-29 11:52:04

标签: javascript node.js express passport.js bcrypt

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

  

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时为何无法得到定义

passport.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);
            }
        }
    ))

1 个答案:

答案 0 :(得分:1)

尝试与user.attributes.password进行比较。