Bcrypt非法参数:数字

时间:2017-12-12 23:48:20

标签: node.js bcrypt

我遇到了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是非法参数,它不是,我已经确保它们的文档中的语法是正确的。

1 个答案:

答案 0 :(得分:5)

我很确定应该是

genSalt(10, function() {})

看起来你有一个错字:)