29

I am trying to save a user to mongodb database using post request as follow, but I got the error bcrypt Error: data and hash arguments required .It's a pretty simple set up of the code but i can't figure out anything wrong with it. models/users.js

const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const confic = require('../models/users');

// User schema
const UserSchema = mongoose.Schema({
 name: {
  type: String,
 },
 email: {
  type: String,
  required: true
 },
 username:{
  type: String,
  required: true
 },
 password: {
  type: String,
  required: true
 }
});

const User = module.exports = mongoose.model('User', UserSchema);

module.exports.getUserById = function(id,callback){
 User.findById(id,callback);
}

module.exports.getUserByUsername = function(username,callback){
 const query = {username:username}
 User.findOne(query,callback);
}

module.exports.addUser= function (newUser, callback) {
   bcrypt.genSalt(10,(err,salt) => {
    bcrypt.hash(newUser.password, salt , (err, hash) =>{
        if(err) throw (err);

        newUser.password=hash;
        newUser.save(callback);
    });
   });
}
routes/users.js

const jwt = require('jsonwebtoken');
User = require('../models/users')

// // Register
router.post('/register', (req, res, next) => {
  var newUser = new User({
    name: req.body.name,
    email: req.body.email,
    username: req.body.username,
    password: req.body.password
  });

  User.addUser(newUser, (err, User) => {
    if(err){
      // res.json({success: false, msg:'Failed to register user'});
    } else {
      // res.json({success: true, msg:'User registered'});
    }

  });

});

// Authenticate
router.post('/authenticate', (req, res, next) => {
  res.send('AUTHENTICATE');
});

// Profile
router.get('/profile', (req, res, next) => {
  res.send('PROFILE');
});

module.exports = router;
Server was running but after using postman chrome post request error are shown and server stop working as errors shown in image.enter image description here

8 Answers8

54

The error comes from the bcrypt.hash method. In your case, you have the following piece of code :

bcrypt.hash(newUser.password, salt , (err, hash) => { ... }

I think that your problem comes from the newUser.password that must be empty (null or undefined). The error says data and salt arguments required. It looks like your salt is correctly generated and you didn't check if newUser.password === undefined, so here's my bet: somehow newUser.password is undefined.

Also, you can check if the genSalt method works fine by adding if(err) throw (err); after calling it as you did for the bcrypt.hash method.

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
boehm_s
  • 5,254
  • 4
  • 30
  • 44
  • And what about logging `newUser` before generating the salt ? – boehm_s Jul 10 '17 at 15:34
  • actually i am not getting how to do that i am just confused abour logging newUsers before generating salt please explain? – Sarbjyot Singh Chahal Jul 10 '17 at 15:37
  • Doing so, you'll be able to check if at this stage of the process, `newUser.password` has a value or if it is `undefined`. Then, if it's `undefined`, the error comes from the data course before getting here – boehm_s Jul 10 '17 at 15:39
  • yes you were right newUsers.password is undefined now what i do to solve this ??? – Sarbjyot Singh Chahal Jul 10 '17 at 15:45
  • You can log `newUser` just after it's creation to see if `newUser.password` is undefined at this moment – boehm_s Jul 10 '17 at 15:48
  • newUser.password is defined every where i am sorry i was confussed but after if(err) throw (err); program is stopped errer is thrown before that newUser.password is defined everywhere before that – Sarbjyot Singh Chahal Jul 10 '17 at 15:57
  • The program doesn't stops because of the `if(err) throw (err) `, it stops because of the `bcrypt.hash` function. Can you log `newUser` before this line : `User.addUser(newUser, (err, User) => { ... }` – boehm_s Jul 10 '17 at 16:05
3

Remove the arrow => in bcrypt.hash() . Use old fashioned method definition function() {}

2

Make sure the properties are as they are being supplied, in my case, I was sending a Password property with a capital P and then passing the password with a small p letter to the hash function.

Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42
1

Per mongoose docs: https://mongoosejs.com/docs/faq.html

Q. I'm using an arrow function for a virtual, middleware, getter/setter, or method and the value of this is wrong.

A. Arrow functions handle the this keyword much differently than conventional functions. Mongoose getters/setters depend on this to give you access to the document that you're writing to, but this functionality does not work with arrow functions. Do not use arrow functions for mongoose getters/setters unless do not intend to access the document in the getter/setter.

1

When I was testing with plain text mode, I got this error (data and salt argument required), once I did change to json, it was ok.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
0

Try installing body parser middleware. Should solve the issue.

Here's how to use it in your code.

Install npm i body-parser

Use as middleware in app.js/server.js,

const bodyparser = require("body-parser");
app.use(bodyparser.urlencoded({ extended: false }));
app.use(bodyparser.json());
profCodey
  • 1
  • 1
0

it seems like the password argument you passed isn't defined. I know the req.body.password is supposed to generate the user password, but for some reasons it doesn't work that way. Instead try this

bcrypt.genSalt(10,(err,salt) => { bcrypt.hash(${password}, salt , (err, hash) =>{ if(err) throw (err) // enter rest of code here

What i did was simply use the iterals in passing the password arguments.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 07 '23 at 20:35
0

Error: data and salt arguments required

{
    "email": "me4@gmail.com",
    "passsword": "djhsd@92129nnj!"
}

It's likely that the password field is not being passed correctly in the request payload. Your code example provides valuable insight into how to properly validate the email and password inputs before attempting to use them.

Sometimes your Postman API checking to go to incorrectly or something this error overcome another wish this error comes from, It looks like your salt is correctly generated and you didn't check newUser.password === undefined

Here example code snippet,

if (!email || typeof email !== "string") {
    throw Error("Email is required");
}

//valide password
if (!password || typeof password !== "string") {
    throw Error("Password is required");
}

//valide email format
if (!validator.isEmail(email)) {
    throw Error("Email is not valid");
}

//valide password length
if (!validator.isLength(password, { min: 6 })) {
    throw Error("Password must be at least 6 characters");
}
Anonymous
  • 835
  • 1
  • 5
  • 21