const userRegistrationPost = (req, res) => {
const{name, email, pass1, pass2} = req.body;
let errors = [];
if(!name|| !email || !pass1 || !pass2){
errors.push({msg: "Please ensure all fields are filled"});
}
if(pass1 !==pass2){
errors.push({msg: "Passwords don't match"});
}
if(pass1.length < 6){ //line 66
errors.push({msg: "Passwords must be at least 6 characters"});
}
if(errors.length > 0){
res.render("user_registration", {errors, name, email, pass1, pass2})
}else{
//WE DON'T WANT TO HAVE 2 USERS WITH THE SAME EMAIL
User.findOne({email: email})
.then((result)=> {
if(result){
errors.push({msg: "Email already exists"});
res.render("user_registration", errors, name, email, pass1, pass2)
}else{
//HERE WE TRY TO ENCRYPT OUR PASSWORD
bcrypt.hash(pass1, 10, (error, hash)=>{
const nUser = new User({
name, email, password: hash,
})
try{
nUser.save();
req.flash('message', "Registration Successful. You can now Signup")
res.redirect('/user_login');
}
catch(err){
req.flash('error_msg', "Could not save into the Database");
res.redirect('user_registration');
}
})
}
})
.catch((err)=> {
res.send("There's a problem");
console.log(err);
})
}
}
It says the error occurs on line 66. It was supposed to save users into the database after inputing their details but it keeps giving me TypeError.
New contributor
Akinsola Oluwatamilore is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.