简体   繁体   English

无法通过承诺从 nodejs 中的 bcrypt.compare() function 返回数据

[英]Can't return data out of the bcrypt.compare() function in nodejs with promises

I'm currently buildung an application with nodejs (10.15.2) but I'm running into some problems with the login function.我目前正在使用 nodejs (10.15.2) 构建应用程序,但在登录 function 时遇到了一些问题。

Snippet from my "dbconnector.js" file:我的“dbconnector.js”文件中的片段:

login(email, password) {
    return userModel.findOne({
      email: email
    }).lean().then(function(user) {
      // return user;
      return bcrypt.compare(password, user.password, function(err, res) {

           if (res) {
              console.log(user); //prints the userinfo - works fine
              return user;
          }
        });
    });
};

Snippet from my serve.js file:我的 serve.js 文件中的片段:

app.post('/login', async (req, res) => {

    var { email, password } = req.body;
    var user = await dbconnector.login(email,password);
    console.log(user) //returns undefined

    if (user != false) {
      console.log("loggedin");
        const accessToken = jwt.sign({ email: user.email,  id: user.id }, key.tokenKey);

        res.json({
            accessToken
        });
    } else {
        res.send('Username or password incorrect');
    }
});

My problem is that the login() -function returns undefined.我的问题是login() -函数返回未定义。 However, when I change it to the following, it works perfect (just without checking the password...):但是,当我将其更改为以下内容时,它可以完美运行(只是不检查密码......):

  login(email, password) {
    return userModel.findOne({
      email: email
    }).lean().then(function(user) {
    return user;
    });
};

So I know something's wrong with the bcrypt / promise part but I couldn't find a solution.所以我知道 bcrypt / promise 部分有问题,但我找不到解决方案。

My Fellow brother, the problem lives in the fact that a promise will only return you another promise and the compare callback that you have there as its name says callback is not apromise.我的兄弟,问题在于 promise 只会返回另一个 promise 并且您在那里拥有的比较回调正如其名称所说的回调不是承诺。 Lets make it a promise让它成为 promise


login(email, password) {
    return userModel.findOne({
      email: email
    }).lean().then(function(user) {
      // return user;
      return new Promise((resolve, reject) => {
          return bcrypt.compare(password, user.password, (err, res) => {
              if (err) {
                  return reject(err);
              }

              return resolve(res);
        })
};

BUT!!!但!!! if by any means the bcrypt package u are using is this one https://www.npmjs.com/package/bcrypt then you dont event have to promisify the method, it has it as promise already如果以任何方式你正在使用的 bcrypt package 是这个https://www.npmjs.com/package/bcrypt那么你不必承诺该方法,它已经有 ZB321DE3BDC299D807E9


login(email, password) {
    return userModel.findOne({
      email: email
    }).lean().then(function(user) {
      return bcrypt.compare(password, user.password);
    }).then(isEquals => {
        if (isEquals) {
            return true
        }

        return false
    }).catch(error => {
        throw error;
    })
};

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

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