NodeJS returns: Illegal arguments: number

-1
   const bodyParser = require('body-parser');
const bcrypt = require('bcryptjs');

//Bring in User Model
let User = require('../models/user');


var urlencodedParser = bodyParser.urlencoded({extended: false});

module.exports = function(app){

  app.get('/signup', function(req, res){
    res.render('signup');
  });

  app.post('/signup', urlencodedParser, function(req, res){
    const fstName = req.body.userName;
    const sncName = req.body.userLastName;
    const email = req.body.userEmail;
    const pass = req.body.userPassword;
    const rePass = req.body.userRePassword;
    const birDay = req.body.userBirDay;
    const birMonth = req.body.userBirMonth;
    const birYear = req.body.userBirYear;
    const gender = req.body.userGender;
    const birthday = birYear + '-' + birMonth + '-' + birDay;

    let newUser = new User({
      firstName: fstName,
      lastName: sncName,
      email: email,
      password: pass,
      birthday: birthday,
      gender:gender
    });

    bcrypt.getSalt(10, function(err, salt){
      bcrypt.hash(newUser.password, salt, function(err, salt){
        newUser.password = hash;

        newUser.save().then(function(){
            res.render('signup');
        });
      });
    });




  });
};

You are returning this error: Illegal arguments: number

    
asked by anonymous 23.11.2017 / 21:19

1 answer

0

Is it your choice not to use bcrypt asynchronously?

Thanks for alerting me, new here on the stack. Anyway, this would be my solution to your problem:

const Promise = require('bluebird');

const bodyParser = require('body-parser');
const bcrypt = Promise.promisifyAll(require('bcrypt-nodejs'));

//Bring in User Model
let User = require('../models/user');

var urlencodedParser = bodyParser.urlencoded({extended: false});

module.exports = function(app){

app.get('/signup', function(req, res){
   res.render('signup');
});

app.post('/signup', urlencodedParser, function(req, res){
   const fstName = req.body.userName;
   const sncName = req.body.userLastName;
   const email = req.body.userEmail;
   const pass = req.body.userPassword;
   const rePass = req.body.userRePassword;
   const birDay = req.body.userBirDay;
   const birMonth = req.body.userBirMonth;
   const birYear = req.body.userBirYear;
   const gender = req.body.userGender;
   const birthday = birYear + '-' + birMonth + '-' + birDay;

   let newUser = new User({
     firstName: fstName,
     lastName: sncName,
     email: email,
     password: pass,
     birthday: birthday,
     gender:gender
   });

   const SALT_FACTOR = 8;

   bscript.genSaltAsync(SALT_FACTOR)
    .then(salt => bcrypt.hashAsync(newUser.password, salt, null))
    .then(hash => {
      newUser.password = hash;

      newUser.save().then(() => res.render('signup'));
    });
  });
};

In addition to making it asynchronous, I also changed bcryptjs through bcrypt-nodejs (in a little project I made I had trouble with it) and installed the bluebird for the promises task. I hope this solves it.

    
23.11.2017 / 23:14