0

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 :)

Ashvin777
  • 1,446
  • 13
  • 19
Corentinrce
  • 331
  • 1
  • 4
  • 8

3 Answers3

1

Seems like you don't wait for the createUser callback function to respond before you verify everything was successful.

GisliBG
  • 21
  • 1
  • 2
0

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');
      })
B. Assem
  • 1,058
  • 9
  • 12
  • same thing :( the form value disappear and i need to click a second time to have the error – Corentinrce Feb 05 '19 at 20:34
  • Can you put a console.log('something') before the req.flash('error_msg','EMAIL ALREADY IN DB'); It seems the findOne is not finding the user the first time – B. Assem Feb 05 '19 at 21:54
  • i think its the req.flash the problem, how can i remplace this ? the console.log work ! – Corentinrce Feb 06 '19 at 05:56
  • check this link : https://stackoverflow.com/questions/23160743/how-to-send-flash-messages-in-express-4-0 – B. Assem Feb 06 '19 at 09:49
0

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');
        })
        }
      })
    }
  });
Corentinrce
  • 331
  • 1
  • 4
  • 8