Nodejs returns: Illegal Arguments: number

Asked

Viewed 109 times

-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');
        });
      });
    });




  });
};

This is returning this error: Illegal Arguments: number

1 answer

0


It is your choice not to use bcrypt asynchronously?

Thanks for alerting me, new here in 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 asynchronous, I also switched the bcryptjs by bcrypt-nodejs (in a little project I did had trouble with it) and installed the Bluebird for the promise task. I hope this resolves.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.