从 bcrypt 哈希函数中获取未定义的值

Gettting an undefined value from bcrypt hash function

好的,我从一个函数中得到了一个未定义的值,我不知道为什么,我正在尝试获取用于插入数据库的密码哈希值,但是具有该函数的常量值为“undefined”,那么我应该在我的代码中更改什么?

async postCompletedetails(req, res) {
  const company = req.params.company;
  const name = req.params.name;
  const password = req.params.password;
  const hashPass = await bcrypt.hash(password, saltRounds, function(err, hash) {
    if (err) {
      throw err
    } else {
      console.log(hash)
    }
  })



  if (
    company !== undefined &&
    name !== undefined &&
    password !== undefined
  ) {

    const {
      token
    } = req.headers;
    const decoded = jwt.verify(token, process.env.JWT_ACCOUNT_ACTIVATION);
    const id = decoded.id;

    const update = await pool.query(
      `UPDATE user SET Name_user= '${name}', password= '${hashPass}' WHERE ID_user = ${id}`
    );
    const incompany = await pool.query(
      `INSERT INTO company (Name_company) VALUES ('${company}') `
    );

    const inrelcompany = await pool.query(
      `INSERT INTO rel_company_user (ID_company, ID_user) VALUES (LAST_INSERT_ID(), ${id})`
    );

    return res.json({
      code: 200,
      message: "todo bien... todo correcto y yo que me alegro",
      hashPass,
      password
    });
  } else {
    return res.json({
      code: 400,
      message: "Bro hiciste algo mal",
    });
  }
}

当您调用 bcrypt.hash() 并传入回调函数时,不会返回任何 Promise。您可以停止该回调,然后您的 await 将按预期工作。

基本上,与许多 API 一样,您可以在“老派”回调函数方法或更现代的 Promise/async 模型之间进行选择。一个或另一个,但不能同时。