如何将Flash消息传递到ejs文件?

时间:2019-01-11 12:56:18

标签: node.js mongodb flash

我想将Flash消息传递到ejs文件中进行注册。我在这里也附上了app.js文件和我的注册ejs文件。我想显示用户在注册时发现任何错误,然后系统会将用户重定向到注册页面,并在Flash消息中显示错误。       

  app.use(flash());
app.use(function(req,res,next){
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
next();
});


 <!-- user.js - Here i check whether error found or not -->
  if(errors)
   {
     res.render('register',{
     errors:errors
    });
   } 



  <!-- Registration form -->
      <div class="col-md-4">

      <section class="register-form">

        <form method="POST" action="/register" >
          <div class="form-group">

          </div>
          <div class="form-group">
          <input type="text" name="name" placeholder="Name"  class="form- 
     control input-lg" />
          </div>
          <div class="form-group">
          <input type="text" name="username" placeholder="Username"  
     class="form-control input-lg" />
          </div>
          <div class="form-group">
          <input type="text" name="email" placeholder="Email"  class="form- 
     control input-lg" />
          </div>
          <div class="form-group">
          <input type="password" name="password" class="form-control input- 
     lg" id="password" placeholder="Password"  />
          </div>
          <div class="form-group">
          <input type="password" name="password2" class="form-control 
    input-lg" id="password2" placeholder="Confirm Password"  />
          </div>
          <div class="pwstrength_viewport_progress"></div>
          <button type="submit" name="submit" class="btn btn-lg btn-primary 
    btn-block">Sign Up</button>
          <div>
            <p> Have an account :  <a href="/login">Login Here </a> </p>
          </div>
        </form>

      </section>
      </div>
    </div>

0 个答案:

没有答案