简体   繁体   中英

Bcrypt illegal argument: number

I'm having issues with bcrypt and I'm not sure why. Here is my code for a basic registration route:

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;

Now the error I'm getting is this:

 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)

Which is why I'm a bit confused. The way the error looks is that it is saying the 10 is the illegal argument, which it isn't, I've made sure that the syntax was correct from their documentation.

I'm pretty sure that should be

genSalt(10, function() {})

Looks like you have a typo :)

convert password to string and try.It may work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM