Friday 17 March 2017

Signup called twice nodejs

I have a problem with my signup method. When I fill the signup form and i click the signup button, the method signup is called two times. In the first one the new user is created, in the second one, the user exists and the form shows an error indicating that the user already exists. I don't know why it is called two times...

Here is the signup method:

signUpUser = function(req, res) {
 log.debug("signUpUser - INIT " + req.body.email );
 // find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'local.email' :  req.body.email }, function(err, user) {

if (err){
  log.debug(" local-signup findOne user: " + user);
  req.flash('signupMessage', 'ERROR');
  res.render('users/signup', {
      message: req.flash('signupMessage')
  });
}

// check to see if theres already a user with that email
if (user) {
    log.debug(" local-signup findOne user: " + user);
    req.flash('signupMessage', 'ACCESS.USER_EXISTS');
    res.render('users/signup', {
        message: req.flash('signupMessage')
    });
} else {

            log.debug(" local-signup findOne newUser.");
            // if there is no user with that email
            // create the user
            var newUser = new User({
              username    : escape(req.body.name),
              gender      : req.body.gender,
              want     : req.body.want,
              location    : [req.body.longitude,req.body.latitude]
            });

            var birth = new Date();
            var params = req.body.birthday.split("/");
            birth.setDate(params[0]);
            birth.setMonth(parseInt(params[1])-1);
            birth.setFullYear(params[2]);

            newUser.birthday    = birth;

            newUser.local.email    = req.body.email;
            newUser.local.password = newUser.generateHash(req.body.password);

            // save the user
            log.debug("before newUser.save");
            newUser.save(function(err) {
                log.debug(" newUser.save");
                if (err){
                  log.debug("err " + err);
                  errorHandler.processError(err, log);
                }else{
                  var locale = req.headers["accept-language"];
                  log.debug("locale " + locale);
                  mailer.sendConfirmationMail(newUser._id,newUser.local.password,newUser.local.email, locale);
                }

                req.flash('signupMessage', 'greeting.welcome');
                res.status(200).render('dashboard/index', {
                  user:newUser,
                  message: req.flash('signupMessage')
                });
            });
}

});
log.debug("signUpUser - END");
};

Here is the form:

<form id="createUserForm" class="form col-md-12 center-block" action="/signup" method="post">
                        <!-- show any messages that come back with authentication -->
                            <% if (message.length > 0) { %>
                                <div class="alert alert-info"><%= message %></div>
                            <% } %>
                         <div class="form-group">
                          <label class="control-label"><h4>* <%= __('SIGNUP.NAME') %></h4></label>
                          <input type="text" name="name" id="name" class="form-control" placeholder="<%= __('SIGNUP.NAME') %>" onchange="validateFormInput(true, 'text', 'name', 3, 25)">
                          <div id="help_name" class="help-block hidden"><%= __('SIGNUP.NAME_INVALID') %></div>
                        </div>
                         <div class="form-group">
                            <label class="control-label"><h4>* <%= __('SIGNUP.BIRTHDAY') %>:</h4></label>
                            <span class='input-group date' id='datetimepicker1' >
                                <input id="birthday" name="birthday" type='text' class="form-control"  onblur="validateFormInput(true, 'date', 'birthday')"/>
                                <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                                </span>
                            </span>
                            <div id="help_birthday" class="help-block hidden"><%= __('SIGNUP.BIRTHDAY_INVALID') %></div>
                        </div>
                        <div class="form-group">
                         <label class="control-label"><h4>* <%= __('SIGNUP.EMAIL') %></h4></label>
                          <input type="text" name="email" id="email" type="email" class="form-control" placeholder="<%= __('SIGNUP.EMAIL') %>" onchange="validateFormInput(true, 'mail', 'email',5,50)" >
                          <div id="help_email" class="help-block hidden"><%= __('SIGNUP.EMAIL_INVALID') %></div>
                          <div class="help-block"><%= __('SIGNUP.EMAIL_NOT_VISIBLE') %></div>
                        </div>
                        <div class="form-group">
                          <label class="control-label"><h4>* <%= __('SIGNUP.PASSWORD') %></h4></label>
                          <input type="password" name="password" id="password" class="form-control" placeholder="Passw" onchange="validateFormInput(true, 'password', 'password', 6, 50)">
                          <div id="help_password" class="help-block hidden"><%= __('SIGNUP.PASSWORD_INVALID') %></div>
                        </div>
                        <div class="form-group">
                          <input type="hidden" name="longitude" id="longitude" class="form-control" val="">
                          <input type="hidden" name="latitude" id="latitude" class="form-control" val="">
                        </div>

                        <div class="form-group">
                          <div class="col-md-6">
                            <label><h4><%= __('SIGNUP.GENDER') %>:</h4></label>
                              <div class="radio">
                                <label>
                                  <input type="radio" name="gender" id="optionsRadios1" value="0" checked>
                                  <%= __('SIGNUP.GENDER_0') %>
                                </label>
                              </div>
                              <div class="radio">
                                <label>
                                  <input type="radio" name="gender" id="optionsRadios2" value="1">
                                  <%= __('SIGNUP.GENDER_1') %>
                                </label>
                              </div>
                          </div>
                          <div class="col-md-6">
                            <label><h4><%= __('SIGNUP.WANT') %>:</h4></label>
                              <div class="radio">
                                <label>
                                  <input type="radio" name="want" id="optionsRadios1" value="1" checked>
                                  <%= __('SIGNUP.WANT_0') %>
                                </label>
                              </div>
                              <div class="radio">
                                <label>
                                  <input type="radio" name="want" id="optionsRadios2" value="2">
                                  <%= __('SIGNUP.WANT_1') %>
                                </label>
                              </div>
                              <div class="radio">
                                <label>
                                  <input type="radio" name="want" id="optionsRadios3" value="3">
                                  <%= __('SIGNUP.WANT_2') %>
                                </label>
                              </div>
                          </div>
                        </div>
                        <div class="form-group" >
                            <button type="button" onclick="submitForm('createUserForm');" class="btn btn-primary btn-block btn-lg"><span class="glyphicon glyphicon-plus"></span>&nbsp;<%= __('BUTTON.CREATE') %></button>
                            <a href="/login" class="btn btn-primary btn-block btn-lg" ><span class="glyphicon glyphicon-log-in"></span>&nbsp;<%= __('SIGNUP.HAVE_ACCOUNT') %></a>
                        </div>

                      </form>

The submitForm method is the following:

function submitForm(formId){
   $("#"+formId).submit();
  }

Here is the log with the debug traces when signup is executed:

{"level":"debug","message":"signUpUser - INIT hhh@uoc.edu","timestamp":"2017-03-17T18:11:11.135Z"} {"level":"debug","message":"signUpUser - END","timestamp":"2017-03-17T18:11:11.136Z"} {"level":"debug","message":" local-signup findOne newUser.","timestamp":"2017-03-17T18:11:11.140Z"} {"level":"debug","message":"before newUser.save","timestamp":"2017-03-17T18:11:11.259Z"} {"level":"debug","message":" newUser.save","timestamp":"2017-03-17T18:11:11.371Z"} {"level":"debug","message":"signUpUser - INIT hhh@uoc.edu","timestamp":"2017-03-17T18:11:11.473Z"} {"level":"debug","message":"signUpUser - END","timestamp":"2017-03-17T18:11:11.474Z"}



via scuencag

No comments:

Post a Comment