简体   繁体   English

为什么我在 bcrypt compare 上遇到错误? 异步/等待函数

[英]why do i get an error on bcrypt compare? async/await function

I'm trying to log a user with nodeJs and MySql我正在尝试使用 nodeJs 和 MySql 登录用户

Here my function to connect a User:这是我连接用户的功能:

export async function connectUser(email, password, cb) {

  console.log('Entry connectuser');
  let db = dbConnect();

  await db.query('SELECT id, email, password FROM user WHERE email = ?', [email], async (error, result) => {
    if (error) return cb(error);

    const user = result[0];
    
    console.log(result);
    console.log(user.email);
    console.log(user.password);
    console.log(user.id);
    
    const isValid = await bcrypt.compare(password, user.password);
    if (isValid === true) {
      const token = jwt.sign({id: user.id, email: user.email }, secret, {expiresIn: 86400 });
      return cb(null, token);
    }
    
    return cb(new Error('Invalid Credentials'));
    
});

I get this error: "UnhandledPromiseRejectionWarning: Error: Illegal arguments: object, string" on the bcrypt.compare()我在 bcrypt.compare() 上收到此错误:“UnhandledPromiseRejectionWarning: E​​rror: Illegal arguments: object, string”

I suppose that it's an error of async/await but i can't fix it... some ideas?我想这是 async/await 的错误,但我无法修复它......一些想法?

As described in the doc , you should use bcrypt.compare like that:文档中所述,您应该像这样使用 bcrypt.compare :

    bcrypt.compare(req.body.password,user.password, function(err, res) {
  if (err){
    // handle error
  }
  if (res)
    // Send JWT
  } else {
    // response is OutgoingMessage object that server response http request
    return response.json({success: false, message: 'passwords do not match'});
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM