TypeError:Object#<incomingmessage>没有方法'flash'</incomingmessage>

时间:2012-08-25 12:18:51

标签: node.js express

使用Node.js和Express时出现以下错误。这是我的版本:

Node : v0.8.8
Express: 3.0.0rc3

尝试访问该页面时获得以下错误:

TypeError: Object #<IncomingMessage> has no method 'flash'
    at routes (/Users/nblavoie/Desktop/HotPie/HotPie/apps/authentication/routes.js:23:8)

我已尝试删除app.use(app.router);从app.js没有成功。这是导致错误的代码。请参阅req.flash line。

app.post('/sessions', function(req, res)
{
    if(req.body.user == 'piechief' 
        && req.body.password == '12345')
    {
        req.session.currentUser = req.body.user;
        req.flash('info', "You're logged in as #{req.session.currentUser}");
        res.redirect("/login");
        return;
    }
    else
    {
        req.flash('error', "Those credentials were incorrect. Try again.");
        res.redirect("/login");
        return;
    }
});

以下是app.js的有趣部分:

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({
    secret: "DBBD6BE563419EDB0E5CBD228E603D4AD232CE05434B4FA95C6908B64EA515C3",
    store: new RedisStore({
      host: "127.0.0.1",
      port: "6379",
      db: "mydb"
    })
  }));
  app.use(app.router);
  app.use(express.static(path.join(__dirname, '/public')));
});

请问好吗?我已经在Express'flash documentation偷了我一眼,我无法看清我做错了什么。

2 个答案:

答案 0 :(得分:26)

req.flashremoved as of 3.0

  
      
  • req.flash()(只使用会话:req.session.messages = ['foo']或类似的)
  •   
  • connect-flash可用作中间件以提供req.flash()
  •   

答案 1 :(得分:6)

这解决了我的问题

https://github.com/jaredhanson/passport/issues/61

现在我的护照app.configure看起来像这样:

app.configure(function (){
    app.use(express.cookieParser('keyboard cat'));
    app.use(express.session({ cookie: { maxAge: 60000 }}));
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(flash());
})