我有问题与bcrypt和我不知道为什么。以下是我的基本注册路线代码:
const express = require('express');
const router = express.Router();
const bcrypt = require('bcryptjs');
const passport = require('passport');
// Register
router.post('/register', function(req, res){
req.checkBody('username', 'Username is Required').notEmpty();
req.checkBody('kingname', 'Kingdom Name is Required').notEmpty();
req.checkBody('email', 'E-Mail is Required').notEmpty();
req.checkBody('password', 'Password is Required').notEmpty();
req.checkBody('password2', 'Passwords do not match').equals(req.body.password);
var errors = req.validationErrors();
if(errors) {
res.render('index', {
errors: errors
});
} else {
var newUser = {
username: req.body.username,
kingname: req.body.kingname,
email: req.body.email,
password: req.body.password,
password2: req.body.password2
}
bcrypt.getSalt(10, function(err, salt){
bcrypt.hash(newUser.password, salt, function(err, hash){
if(err) {
console.log(err);
}
newUser.password = hash;
let post = {username: newUser.username, password: newUser.password, email: newUser.email, kingname: newUser.kingname};
let sql ='INSERT INTO users SET ?';
let query = db.query(sql, post, function(err, res){
if(err) {
console.log(err);
return;
} else {
user_id = res.insertID;
console.log('Last User ID', res.insertID);
console.log('User registration successful...');
res.redirect('/');
}
});
});
});
}
});
module.exports = router;
现在我得到的错误是这样的:
Error: Illegal arguments: number
at Object.bcrypt.getSalt (/home/kogadmin/www/node_modules/bcryptjs/dist/bcrypt.js:339:19)
at /home/kogadmin/www/routes/reg.js:30:10
at Layer.handle [as handle_request] (/home/kogadmin/www/node_modules/express/lib/router/layer.js:95:5)
at next (/home/kogadmin/www/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/kogadmin/www/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/kogadmin/www/node_modules/express/lib/router/layer.js:95:5)
at /home/kogadmin/www/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/kogadmin/www/node_modules/express/lib/router/index.js:335:12)
at next (/home/kogadmin/www/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/home/kogadmin/www/node_modules/express/lib/router/index.js:174:3)
所以我有点困惑。这个错误看起来是说10是非法的参数,实际上不是,我已经从他们的文档中确保了语法是正确的。
2条答案
按热度按时间n3schb8v1#
我很确定那应该是
看起来你有一个错字:)
ie3xauqp2#
将密码转换为字符串并尝试。它可能会工作。