connect-flash返回要求的flash,未定义

问题描述 投票:0回答:1

我目前正在尝试使用connect-flash构建应用,以使用会话(或至少据我所知)保存从一条路由到另一条路由的数据。

有问题的应用程序可以在本地正常运行,但是一旦我部署了该应用程序,就会收到错误消息,其中Flash对象返回未定义状态。

我已尽可能将其剥离。我的app.js如下。

var express = require('express')
  , flash = require('connect-flash')
  , util = require('util');


var app = express();
app.configure(function() {
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.set('trust proxy', 1);
  app.use(express.logger());
  app.use(express.cookieParser('keyboard cat'));
  app.use(express.session({
      key: 'sid',
      cookie: { maxAge: 60000 }
  }));

  // Use connect-flash middleware.  This will add a `req.flash()` function to
  // all requests, matching the functionality offered in Express 2.x.

  app.use(flash());
  app.use(app.router);
});

app.get('/', function(req, res){
  res.render('index', { message: req.flash('info') }); //here I pass the flash object to the view
});

app.get('/scrape', function(req, res){
  req.flash('info', 'Hi there!')
  res.redirect('/');
});

app.get('/no-flash', function(req, res){
  res.redirect('/');
});

app.listen(process.env.PORT);

我的唯一视图具有以下代码。

<!DOCTYPE html>
<html>
    <head>
        <title>Express 3.x Example for connect-flash</title>
    </head>
    <body>

<p>
<a href="/scrape">Scrape</a> | 
<a href="/no-flash">No Flash</a>
</p>

<% if (message) { %>
<p><%= message %></p>
<% } %>

    </body>
</html>

此代码摘自connect-flash文档中给出的示例

我感觉可能需要做一些额外的配置,非常感谢您的帮助。

编辑:奇怪的是,req.flash似乎仍然存在,但其内容却没有。

重定向控制台之前记录的req.flash给出:

: [Function: _flash]

并且在重定向之后它产生:

: [Function: _flash]

但是我将其设置为objData的行为如下。

之前:

: objData (before):
: {
: EmailAddresses: [ '[email protected]' ],
: PhoneNumbers: [ 068541864,' ],
: github: []
: }
: }

以及之后:

: []
node.js express express-session
1个回答
0
投票

我调试此步骤的第一步是在本地主机和生产环境中简单地进行console.log(req),并查看两个环境是否都将flash属性附加到请求上。

© www.soinside.com 2019 - 2024. All rights reserved.