我实现了这样的req-flash:https://www.npmjs.com/package/req-flash。 执行此操作后,将出现以下错误:
D:\_SOURCES\smfc\node_modules\req-flash\index.js:36
if (options.locals) {
^
TypeError: Cannot read property 'locals' of undefined
at module.exports (D:\_SOURCES\smfc\node_modules\req-flash\index.js:36:13)
at Object.<anonymous> (D:\_SOURCES\smfc\routes\index.js:6:13)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (D:\_SOURCES\smfc\app.js:15:14)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (D:\_SOURCES\smfc\bin\www:7:11)
这是来自req-flash的index.js的一部分:
module.exports = function(options) {
if (options.locals) {
localsKey = options.locals;
}
return flash;
};
这是我app.js的介绍:
var mongoose = require('mongoose');
require('./models/SupermarketGroups');
mongoose.connect('mongodb://smfc:smfc@ds011482.mlab.com:11482/smfc');
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var bodyParser = require('body-parser');
var flash = require('req-flash');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(session({ secret: '123' }));
app.use(flash());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
我的代码出了什么问题?
答案 0 :(得分:1)
似乎 req-flash 模块中存在错误。 现在你可以使用它:
app.use(flash({}));
或 您可以在node-modules文件夹中打开req-flash并使用以下命令更新index.js文件:
module.exports = function(options) {
if (options != null && options.locals) {
localsKey = options.locals;
}
return flash;
};
<强>解释强>
因为它没有处理选项的未定义大小写,我们在实例化app.use(flash())
时没有通过。所以,只需检查选项或将空对象传递给它。