如何解决:“错误:需要数据和哈希参数”

时间:2019-06-27 10:35:47

标签: mysql node.js express bcrypt

我正在尝试使用已在表单中注册的用户登录,但出现此错误:

  

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

由于使用了bcrypt,该用户已使用用户名,全名和哈希密码在我的数据库中注册。

const pool = require("./pool");
const bcrypt = require("bcrypt");

function User() {}

User.prototype = {
  find: function(user = null, callback) {

  if (user) {
      var field = Number.isInteger(user) ? "id" : "username";
    }
    let sql = `SELECT * FROM users WHERE ${"" + field + ""} = ?`;
    pool.query(sql, user, function(err, result) {
      if (err) console.log(err);
      callback(result);
    });
  },

  create: function(body, callback) {
    let pwd = body.password;
    body.password = bcrypt.hashSync(pwd, 10);

    var bind = [body.username, body.fullname, body.password];
    console.log(bind);

    let sql =
      "INSERT INTO users(username, fullname, password) VALUES (?, ?, ?)";

    pool.query(sql, bind, function(err, lastId) {
      if (err) throw err;
      callback(lastId);
    });
  },

  login: function(username, password, callback) {
    this.find(username, function(user) {
      if (user) {
        if (bcrypt.compareSync(password, user.password)) {
          callback(user);
          return;
        }
      }
    });
  }
};

module.exports = User;

一旦我按表单中的登录按钮,这就是我得到的错误

  throw err; // Rethrow non-MySQL errors
  ^
    at Object.compareSync (/Users/me/happy/node_modules/bcrypt/bcrypt.js:167:15)
    at /Users/me/happy/core/user.js:46:20
    at Query.<anonymous> (/Users/me/happy/core/user.js:16:7)
    at Query.<anonymous> (/Users/me/happy/node_modules/mysql/lib/Connection.js:525:10)
    at Query._callback (/Users/me/happy/node_modules/mysql/lib/Connection.js:491:16)
    at Query.Sequence.end (/Users/me/happy/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
    at Query._handleFinalResultPacket (/Users/me/happy/node_modules/mysql/lib/protocol/sequences/Query.js:139:8)
    at Query.EofPacket (/Users/me/happy/node_modules/mysql/lib/protocol/sequences/Query.js:123:8)
    at Protocol._parsePacket (/Users/me/happy/node_modules/mysql/lib/protocol/Protocol.js:291:23)
    at Parser._parsePacket (/Users/me/happy/node_modules/mysql/lib/protocol/Parser.js:433:10)

3 个答案:

答案 0 :(得分:1)

确保您在两个参数中都传递了有效值。 userpassword 和 dbpassword 不能是 undefined 或 null

答案 1 :(得分:0)

尝试同步生成盐,更新此行

body.password = bcrypt.hashSync(pwd, 10);

body.password = bcrypt.hashSync(pwd, bcrypt.genSaltSync(10));

答案 2 :(得分:0)

请检查password,并且user.password不等于undefined。因为相同的功能对我来说很好用。 如果仍然遇到相同的问题,请尝试bcrypt.comparebcrypt.compare采用3个参数passwordToCheck,passwordHash和回调。

login: function(username, password, callback) {
    this.find(username, function(user) {
        if (user) {
            bcrypt.compare(password,  user.password, function(err, match) {
                if (err) throw new Error(err);
                else if (match == false) {
                    return res.json({
                        success: false,
                        message: 'Wrong Password'
                    })
                } else {
                    callback(user);
                    return;
                }
            });
        }
    });
};