我收到一个 bcrypt 错误,指出需要数据和哈希参数,引用我的 routes.js 文件中的第 44 行。据我所知,我正在传递该信息:bcrypt.compare 的第一个参数是用户输入的密码,第二个是从数据库中检索到的散列密码。我做错了什么?
bcrypt.compare(req.params.password, user.password, function...
routes.js
'use strict'
var express = require('express');
var router = express.Router();
var User = require('../app/models/user');
//password hashing
var bcrypt = require('bcrypt');
var count = 0;
router.use(function(req, res, next) {
count++;
console.log('API hit count = %s', count);
next();
});
// /users post(create new user) get(specific user)
router.route('/users')
.post(function(req,res) {
var user = new User();
user.username = req.body.username;
user.password = bcrypt.hashSync(req.body.password, 10);
//save the user and checkfor errors
user.save(function(err) {
if (err) {
res.send(err);
} else {
res.json({message: "User created!"});
}
});
})
router.route('/users/:username')
.get(function(req, res) {
var query = {
username: req.params.username,
};
User.findOne(query, function(err, user) {
if (err) {
res.send(err);
} else {
bcrypt.compare(req.params.password, user.password, function(err, res) {
if(err) {
console.log('Comparison error: ', err);
}
})
res.json(user);
}
});
})
最佳答案
bcrypt.compare
有 3 个参数; passwordToCheck、passwordHash 和一个回调。 (Check the documentation for examples)
此错误表示前 2 个参数中的一个或两个为空或未定义。因此,请确保它们都正确传递。 (不是 null 或 undefined)
关于javascript - 加密错误 : data and hash arguments required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42241113/