Hello i have a problem with adding user to database. The problem is when i try to hash password, there's an error that says the password is empty. If i send req.body.password it works. Problem appears only with const hashed_password
Here's my code
const addUser = async (req, res) => {
const { email, password, firstName, lastName, sex, rating, telephone, role, lastSeen } = req.body;
const salt = await bcrypt.genSalt(10);
const hashed_password = await bcrypt.hash(password, salt);
try {
//checking for duplicated phone or email
const user = await User.findOne({ email });
const phoneNumber = await User.findOne({ telephone });
if (user) {
return res.status(400).json({ message: 'User email already exists' });
}
else if (phoneNumber) {
return res.status(400).json({ message: "Phone number already exists" })
}
console.log(hashed_password);
const result = await User.create({
email, hashed_password, firstName, lastName, sex, rating, telephone, role, lastSeen
});
res.status(201).json({ message: 'User created', user: result });
} catch (error) {
console.log(error);
}
}
Error message:
password: ValidatorError: Path password
is required.
CodePudding user response:
You are sending a object, and it requires a password.
Please edit as follows
const result = await User.create({
email, password: hashed_password, firstName, lastName, sex, rating, telephone, role, lastSeen
});