data and salt required : hashSync

113 views Asked by At

i'm trying to hash the password inside the ueserRouter with hashSync to create a signup the error: data and salt are required if someone can help how can i declare data and salt in this function

the code below:

const userRouter = express.Router();

userRouter.post('/signup',expressAsyncHandler(async (req, res) => {

const newUSer = new User({
  name: req.body.name,
  email: req.body.email,
  password: bcrypt.hashSync(req.body.password)
});
const user = await newUSer.save();
res.send({
  _id: user._id,
  name: user.name,
  email: user.email,
  isAdmin: user.isAdmin,
  token: generateToken(user),
});

}) );

2

There are 2 answers

0
laroussi torki On BEST ANSWER

The problem that i was defining password as a number in userSchema so I changed it to string. Then I made this change in userRouter:

password: bcrypt.hashSync(req.body.password, 10);

Instead of:

password: bcrypt.hashSync(req.body.password)
0
JBallin On

Error message:

data and salt are required

Fix by passing num salt rounds (or generated salt):

hashSync(req.body.password, 10)

Source: npm