Error: data and salt arguments required

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.

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
		type: String,
		required: true
	password: {
		type: String,
		required: true

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

module.exports.getUserById = function(id,callback){

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

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



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

// // Register'/register', (req, res, next) => {
  var newUser = new User({
    username: req.body.username,
    password: req.body.password

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



// Authenticate'/authenticate', (req, res, next) => {

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

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

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.

Read More:   Basic webpack not working for button click function - Uncaught Reference error: undefined

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

Per mongoose docs:

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.

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.

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.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts