为什么当我在 req.flash 之后执行 res.redirect 时 Pug 没有显示任何消息?
Why Pug isn't showing any message when I do res.redirect after req.flash?
我正在使用 PUG 作为模板引擎在 Node, Express 中创建一个简单的 CRUD。每当操作完成或失败时,我都会在 .js 文件中使用 req.flash("name","message")
,然后我使用 res.redirect 到特定页面。
但是重定向后,我看不到要在警报中显示的消息。
最初,我对这个问题有同样的问题:
但是在我修改我的代码到解决方案之后,我仍然没有收到任何消息。
这是我的 Index.js 代码:
...
const flash = require('connect-flash');
...
...
...
app.use(flash());
app.use((req, res, next) => {
res.locals.errs = req.flash("error");
res.locals.infos = req.flash("info");
next();
});
...
...
...
这是我在 POST 方法中使用 req、res 的代码,用于将文档保存到 MongoDB 中的集合:
Category.findOne({title: req.body.title}, (err, doc)=>{
if(doc){
res.render('admin/add_category', {
errors: [{msg: 'Category already exists.'}],
category: req.body.title
});
}
else{
var cat = new Category({
title: req.body.title,
slug: req.body.title.replace(/\s+/g, '-').toLowerCase()
})
cat.save((err)=>{
if (err) return console.log(err);
req.flash('info', 'category added'); //doesn't work
res.redirect('/categories');
})
}
这是我的哈巴狗来展示闪光灯。
if infos
for info in infos
div.alert.alert-info #{ info }
if errs
for error, i in errs
div.alert.alert-danger #{ error }
这个用于显示 flash 消息的 pug 文件称为 nav.pug,它使用 include ../includes/nav.pug
包含在主页中
我只想确保消息在重定向后显示在页面上。但事实并非如此。
看你的代码并不清楚,但你是否使用了 connect-flash GitHub page 上推荐的 cookieParser 和会话中间件?
var flash = require('connect-flash');
var app = express();
app.configure(function() {
app.use(express.cookieParser('keyboard cat'));
app.use(express.session({ cookie: { maxAge: 60000 }}));
app.use(flash());
});
问题是 Express 默认是无状态的,所以当你指示浏览器重定向时,它会向 /categories 发送一个新的请求,但是 Express 无法知道这个请求是同一个请求的一部分"session",因此无法加载即显信息。
在阅读了 Nick Gagne 的回答后,我试图了解会话。
事实证明我需要更改我的快速会话中间件设置。因为 cookies: { secure: true}
网站需要在 https 中 运行,但不幸的是我 运行 本地主机上的服务器,这意味着当我更改为 cookies { secure: false }
时,将显示 flash 消息。
最终代码:
app.use(session({
secret: 'secret',
resave: false,
saveUninitialized: true,
cookie: {secure: false }
}));
我正在使用 PUG 作为模板引擎在 Node, Express 中创建一个简单的 CRUD。每当操作完成或失败时,我都会在 .js 文件中使用 req.flash("name","message")
,然后我使用 res.redirect 到特定页面。
但是重定向后,我看不到要在警报中显示的消息。
最初,我对这个问题有同样的问题:
但是在我修改我的代码到解决方案之后,我仍然没有收到任何消息。
这是我的 Index.js 代码:
...
const flash = require('connect-flash');
...
...
...
app.use(flash());
app.use((req, res, next) => {
res.locals.errs = req.flash("error");
res.locals.infos = req.flash("info");
next();
});
...
...
...
这是我在 POST 方法中使用 req、res 的代码,用于将文档保存到 MongoDB 中的集合:
Category.findOne({title: req.body.title}, (err, doc)=>{
if(doc){
res.render('admin/add_category', {
errors: [{msg: 'Category already exists.'}],
category: req.body.title
});
}
else{
var cat = new Category({
title: req.body.title,
slug: req.body.title.replace(/\s+/g, '-').toLowerCase()
})
cat.save((err)=>{
if (err) return console.log(err);
req.flash('info', 'category added'); //doesn't work
res.redirect('/categories');
})
}
这是我的哈巴狗来展示闪光灯。
if infos
for info in infos
div.alert.alert-info #{ info }
if errs
for error, i in errs
div.alert.alert-danger #{ error }
这个用于显示 flash 消息的 pug 文件称为 nav.pug,它使用 include ../includes/nav.pug
我只想确保消息在重定向后显示在页面上。但事实并非如此。
看你的代码并不清楚,但你是否使用了 connect-flash GitHub page 上推荐的 cookieParser 和会话中间件?
var flash = require('connect-flash');
var app = express();
app.configure(function() {
app.use(express.cookieParser('keyboard cat'));
app.use(express.session({ cookie: { maxAge: 60000 }}));
app.use(flash());
});
问题是 Express 默认是无状态的,所以当你指示浏览器重定向时,它会向 /categories 发送一个新的请求,但是 Express 无法知道这个请求是同一个请求的一部分"session",因此无法加载即显信息。
在阅读了 Nick Gagne 的回答后,我试图了解会话。
事实证明我需要更改我的快速会话中间件设置。因为 cookies: { secure: true}
网站需要在 https 中 运行,但不幸的是我 运行 本地主机上的服务器,这意味着当我更改为 cookies { secure: false }
时,将显示 flash 消息。
最终代码:
app.use(session({
secret: 'secret',
resave: false,
saveUninitialized: true,
cookie: {secure: false }
}));