I was learning JWT by following a YouTube tutorial. When i started hashing the password it threw me this error. I printed the salt it's printing fine but there's some issue while hashing.
I tried for try catch method too but it automatically went to the catch part.
The error is as follows:
https://i.stack.imgur.com/KJVPk.png
const User = require('../models/User');
const {registerValidation} = require('./validation');
const bcrypt = require('bcryptjs');
router.post("/register", async (req, res) => {
//VALIDATE BEOFRE ADDING USER
const {error} = registerValidation(req.body);
if(error) return res.status(400).send(error.details[0].message);
//Checking user is already in the database
const emailExist = await User.findOne({email: req.body.email});
if(emailExist) return res.status(400).send("Email already exists");
const salt = await bcrypt.genSalt(10);
const hashPassword = await bcrypt.hash(req.body.passowrd, salt);
//Create a new user
const user = new User({
name: req.body.name,
email: req.body.email,
password: hashPassword
});
try {
const savedUser = await user.save();
res.send(savedUser);
} catch(err) {
console.log(err);
res.status(400).send(err);
}
});
module.exports = router;