简体   繁体   English

req.flash不是Nodejs的功能

[英]req.flash is not a function Nodejs

I have been staring at this code for the past hour and I don't know how to fix the problem, I'm getting 'TypeError: req.flash is not a function . 在过去的一个小时中,我一直在盯着这段代码,但我不知道如何解决该问题,我收到的是'TypeError: req.flash is not a function I have tried moving app.use(flash) around and still I'm getting the error. 我已经尝试过移动app.use(flash) ,但仍然出现错误。 How can I fix this? 我怎样才能解决这个问题?

var flash = require('connect-flash');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var handlebars = require('express-handlebars').create({
      layoutsDir: path.join(__dirname, "views/layouts"),
      partialsDir: path.join(__dirname, "views/partials"),
      defaultLayout: 'layout',
      extname: 'handlebars'
});
var expressValidator = require('express-validator');
var session = require('express-session');
var passport = require('passport');
var localStrategy = require('passport-local').strategy;
var mongo = require('mongodb');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/login');
var db = mongoose.connection;

// init app
var app = express();

var routes = require('./routes/index');
var users = require('./routes/users');

// view engine
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('views', path.join(__dirname, "views"));

app.use(expressValidator());

// bodyParser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

// set static folder
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);

app.use(flash);

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

// passport init
app.use(passport.initialize());
app.use(passport.session());

// Global Vars
app.use(function (req, res, next) {
    res.local.success_msg = req.flash('success_msg');
    res.local.error_msg = req.flash('error_msg');
    res.local.error = req.flash('error');
    next();
});

// set Port
app.set('port', (process.env.PORT || 3000));
app.listen(app.get('port'), function () {
    console.log('Server started on port ' + app.get('port'));
});

Per the documentation , change this: 根据文档 ,更改此:

app.use(flash);

to this: 对此:

app.use(flash());

And, move your app.use(flash()) to be before your other route definitions, but after your session definitions. 并且,将app.use(flash())移到其他路由定义之前,但在会话定义之后。

The middleware you set up with app.use() is being executed in the order it's declared, so in your case the flash-module will be called before session-handeling. 您使用app.use()设置的中间件将按照声明的顺序执行,因此在您的情况下,将在会话处理之前调用flash模块。 A quick look into the connect-flash-docs reveals that app.use(flash()); 快速浏览connect-flash-docs会发现app.use(flash()); should be called after the other session-related middleware is setup, so try moving it down some lines, that should fix your problem. 应该在设置了其他与会话相关的中间件之后调用它,因此请尝试将其向下移动几行,这应该可以解决您的问题。

Quote from the connect-flash-docs: 引用connect-flash-docs:

Flash messages are stored in the session. Flash消息存储在会话中。 First, setup sessions as usual by enabling cookieParser and session middleware. 首先,通过启用cookieParser和会话中间件,照常设置会话。 Then, use flash middleware provided by connect-flash. 然后,使用connect-flash提供的Flash中间件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM