Flash 消息不工作 express/nodejs/ejs [英] Flash Messages are not working express/nodejs/ejs

查看:68
本文介绍了Flash 消息不工作 express/nodejs/ejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Flash 消息似乎不起作用,我想我遗漏了一些非常明显的东西,但我已经处理了一个小时,但我仍然不知道为什么它不起作用.

我的中间件:

//会话中间件app.use(会话({秘密:'酿百吉饼',重新保存:真的,saveUninitialized: 真}));//快递消息中间件app.use(require('connect-flash')());app.use(function(req, res, next) {res.locals.messages = require('express-messages')(req, res);下一个();});

应用路由,仅用于测试目的.

//使用 express-flash 模块创建 flash 消息的路由app.all('/express-flash', function( req, res ) {req.flash('success', '这是使用 express-flash 模块的 flash 消息.');res.redirect(301, '/');});

模板(直接从他们的github复制)

<% Object.keys(messages).forEach(function (type) { %><ul class="<%= type %>"><%messages[type].forEach(function(message){%><li><%=消息%></li><%})%><%})%>

我是如何渲染的

<%- messages('msg_temp', locals) %>

现在我不确定出了什么问题,我已经尝试了我在这里找到的所有可能的修复方法,并通过谷歌,但它似乎并不想工作.

解决方案

你需要设置一个像 error/success 这样的键的消息

req.flash('success', "hello flash");

并且您将在 res.locals 中获得相同的密钥,或者可以在渲染视图时说

 {% 如果成功 %}<div class="alert alert-success alert-dismissable"><button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>{{ 成功 }}

{% 万一 %}

注意:会话 cookie 中的闪存存储密钥,消息仅刷新一次

Flash messages seem to not be working and I think I am missing something super obvious, but I've been at it an hour, and I still have no idea on why it is not working.

My Middleware:

// Session middleware
app.use(session({
  secret: 'stuffedbagels',
  resave: true,
  saveUninitialized: true
}));

// Express Messages middleware
app.use(require('connect-flash')());
app.use(function(req, res, next) {
  res.locals.messages = require('express-messages')(req, res);
  next();
});

App route, just for testing purposes.

// Route that creates a flash message using the express-flash module
app.all('/express-flash', function( req, res ) {
    req.flash('success', 'This is a flash message using the express-flash module.');
    res.redirect(301, '/');
});

Template (copied right from their github)

<div id="messages">
<% Object.keys(messages).forEach(function (type) { %>
  <ul class="<%= type %>">
  <% messages[type].forEach(function (message) { %>
    <li><%= message %></li>
  <% }) %>
  </ul>
<% }) %>
</div>

How I'm rendering it

<%- messages('msg_temp', locals) %>

Now I'm not sure what's wrong, and I've tried every possible fix I found on here, and through google, but it just doesn't seem to to want to work.

解决方案

you need to set message with a key like error/success

req.flash('success', "hello flash");

and you'll get same key in your res.locals or can say while rendering on view like

            {% if success %}
            <div class="alert alert-success alert-dismissable">
                    <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                 {{ success }}
            </div>
{% endif %}

NOTE: flash store key in session cookie, message flushed only once

这篇关于Flash 消息不工作 express/nodejs/ejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆