Flash消息未显示在node.js

时间:2018-07-15 15:18:15

标签: node.js express ejs connect-flash

我无法收到即时消息。这是我在路线中给他们打电话的方式:

middlewareObj.isLoggedIn = function(req, res, next){
    if(req.isAuthenticated()){
        return next();
    }
    req.flash("error", "You must be logged in to do that.");
    res.redirect("/login");
};

这是我相关的app.js设置:

var express                 = require("express"),
    app                     = express(),
    bodyParser              = require("body-parser"),
    mongoose                = require("mongoose"),
    passport                = require("passport"),
    flash                   = require("connect-flash");

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

我的模板代码如下:

<% if(error && error.length > 1){ %>
    <%console.log(error)%>
    <div class="alert alert-danger" role="alert">
        <%= error %>
    </div>
<% } %>

完全卡住。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

<% if(error && error.length > 1){ %>

需要成为

<% if(error && error.length >0){ %>