如何修复:“错误:需要数据和哈希参数”
Posted
技术标签:
【中文标题】如何修复:“错误:需要数据和哈希参数”【英文标题】:How to fix : " Error: data and hash arguments required " 【发布时间】:2019-11-09 08:45:54 【问题描述】:我正在尝试使用已在表单中注册的用户登录,但出现以下错误:
错误:需要数据和哈希参数
感谢 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)
【问题讨论】:
【参考方案1】:确保您在两个参数中都传递了有效值。 userpassword 和 dbpassword 不应为 undefined 或 null
【讨论】:
【参考方案2】:尝试同步生成盐,更新这一行
body.password = bcrypt.hashSync(pwd, 10);
为
body.password = bcrypt.hashSync(pwd, bcrypt.genSaltSync(10));
【讨论】:
不起作用:/ 我认为我的 compareSync 参数中缺少某些内容【参考方案3】:请检查password
和user.password
不等于未定义。因为同样的功能对我来说很好用。
如果您仍然面临同样的问题,请尝试bcrypt.compare
。 bcrypt.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;
);
);
;
【讨论】:
以上是关于如何修复:“错误:需要数据和哈希参数”的主要内容,如果未能解决你的问题,请参考以下文章
如何修复错误; '错误:引导工具提示需要 Tether (http://github.hubspot.com/tether/)'