Error: data and salt arguments required
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.
Remove the arrow =>
in bcrypt.hash() . Use old fashioned method definition function() {}
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.
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.