I have a problem during the registration with my form, when I click on submit with all the field completed and an email(already in DB) the form is reset and on the second click the error message (email already in DB and other msg...) appear.. what the problem?
router.post('/register', function(req, res) {
var name = req.body.name;
var email = req.body.email;
var username = req.body.username;
var password = req.body.password;
var password2 = req.body.password2;
req.checkBody('username', 'Veuillez renseigner un identifiant').notEmpty();
req.checkBody('email', 'Veuillez renseigner une adresse email valide').isEmail();
req.checkBody('password', 'Veuillez renseigner un mot de passe').notEmpty();
req.checkBody('password2', 'Les mots de passe ne correspondent pas').equals(req.body.password);
var errors = req.validationErrors();
if(errors) {
res.render('register', {errors:errors});
} else {
User.findOne({ email: email }).then(user => {
if (user) {
req.flash('error_msg','EMAIL ALREADY IN DB');
res.render('register', {errors:errors});
} else {
var newUser = new User({
email: email,
username: username,
password: password
});
User.createUser(newUser, (err,user) => {
if(err) throw err;
console.log(user)
})
req.flash('success_msg','u can now log u account is created')
res.redirect('/users/login')
}
});
}
});
Thanks for helping :)