0

我一直在尝试找出一种在我的 js 文件中传递 req.flash() 链接的方法,而实际上并没有在 html 模板中这样做,因为我在其他 html 页面中使用警报消息作为部分,我希望通过在重新发送验证链接中,有没有办法解决这个问题,或者我真的必须创建另一个 req.flash 来验证

app.use(function(req,res,next){
    res.locals.error = req.flash("error");
    res.locals.success = req.flash("success")
    next();
})

router.get("/verification", function(req,res){
    verification coding stuff here
})

验证路线是我希望在 req.flash('error') 中传递的链接

router.get('/verify/:token', function(req,res){
  User.findOne({ verificationToken: req.params.token, resetPasswordExpires: { $gt: Date.now()}}, function(err, user){
      if(err){
          console.log(err);
          //here is where i wish to pass in the link for verification link//
          req.flash('error', 'Mail verify token is invalid or has expired' + resend link here + '.')
          return res.redirect('/forgot')
      } else {
          user.active = "true"
          user.save()
          console.log(user.active);
          req.flash('success', 'Your mail have been verified successfully')
          res.redirect('/login');
      }
  })
})

<% if(error && error.length > 0) { %>
    <div class="alert alert-danger alert-dismissible deposit-alert" role="alert">
        <div class="container">
            <%= error %>
        </div>
    </div>
<% } %>
4

1 回答 1

3

以下内容应该适合您。

控制器::

router.get('/verify/:token', function(req,res){
  User.findOne({ verificationToken: req.params.token, resetPasswordExpires: { $gt: Date.now()}}, function(err, user){
      if(err){
          console.log(err);
          //here is where i wish to pass in the link for verification link//
          var link  = '/verification/';
          var message =  'Mail verify token is invalid or has expired, <a href="'+ link +'"> Click here to resend the link</a>.'
          req.flash('error',message)
          return res.redirect('/forgot')
      } else {
          user.active = "true"
          user.save()
          console.log(user.active);
          req.flash('success', 'Your mail have been verified successfully')
          res.redirect('/login');
      }
  })
})

模板 而不是使用<%= message %> 使用这个。<%- message %>

<% if(error && error.length > 0) { %>
    <div class="alert alert-danger alert-dismissible deposit-alert" role="alert">
        <div class="container">
          <%- message %> 
        </div>
    </div>
<% } %>
于 2018-03-30T12:35:07.213 回答