Reputation: 381
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 :)
Upvotes: 0
Views: 97
Reputation: 381
Update ! it work like this but when i enter a duplicate email but not the password there is just the email error msg, all work except this
This is my register file
{{#if errors}}
{{#each errors}}
<div class="alert alert-danger">{{msg}}</div>
{{/each}}
{{/if}}
{{#if error}}
{{#each error}}
<div class="alert alert-danger"></div>
{{/each}}
{{/if}}
<h2 class="page-header">Register</h2><br>
<form action="/users/register" method="post">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" placeholder="Username" name="username">
</label>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" placeholder="Email" name="email">
</label>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" placeholder="Password" name="password">
</label>
</div>
<div class="form-group">
<label for="password2">Confirm Password</label>
<input type="password" class="form-control" placeholder="Password" name="password2">
</label>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
and my users.js file (not full)
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();
var error = 'email not available'
if(errors) {
res.render('register', {errors: errors})
} else {
User.findOne({ email: email })
.then(user => {
if(user) {
//USER EXIST
res.render('register', {errors: errors, error: error})
} 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');
})
}
})
}
});
Upvotes: 0
Reputation: 1078
Like GisliBG indicated, you must wait for the CreateUser callback ; that means, you need to put the code that needs to be executed after the createUser finish its work, in the callback : this is a general rule/behavior to respect with Asynchronous code:
Try this:
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');
})
Upvotes: 0
Reputation: 21
Seems like you don't wait for the createUser callback function to respond before you verify everything was successful.
Upvotes: 1