1

i'm new in node and express. i'm trying to make a register page using connect-flash for validation and using ejs template engine . everything is working correct but nothing displayed in my alert box. here is my registerController.js :

showRegisterForm(req, res) {
    
    res.render("auth/register", { messages: req.flash("errors"), recaptcha: this.recaptcha.render() }); 
  }

  registerProccess(req, res, next) {
    this.validationData(req)
    .then(result => {
      if(result){res.json('register Proccess')}
      else{res.redirect(req.url)};
       
    })
  };

  validationData(req) {
    req.checkBody("name", "name cant be empty").notEmpty();
    req.checkBody("name", "name must be more than 2 characters").isLength({ min: 2 });
    req.checkBody("password", "password must be more than 8 characters").isLength({ min: 8 });
    req.checkBody("password", "password field cant be empty").notEmpty();
    req.checkBody("email", "Email is not valid").isEmail();

    return req
      .getValidationResult()
      .then(result => {
        const errors = result.array();
        const messages = [];
        errors.forEach((err) => messages.push(err.msg));
        if (errors.length == 0) {
          return true;
        } else {
          req.flash("errors", messages);
          return false;
        }
      })
      .catch((err) => console.log(err));
  }
  register(req, res, next) {
    passport.authenticate('local.register', {
      successRedirect : '/',
      failureRedirect : '/register',
      failureFlash : true
    })(req, res, next)
  }
}
module.exports = new registerController();

and this is my ejs :

<% if(messages.length > 0) { %> 
  <div class="alert alert-danger alert-dismissible">
     <button type="button" class="close" data-dismiss="alert" aria-label="Close">
       <span>×</span>
      </button>                            
      <% messages.forEach(error => { %>
         <%= error %><br>
             <% }) %>
                                
          </div>
        <% } %>

the problem is displaying errors and my code is working correct but nothing displayed in alert section .

0 Answers0