Expressjs/AngularJS:实现req-flash后出错
Expressjs / AngularJS: Error after implementing req-flash
我这样实现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);
我的代码出了什么问题?
似乎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())
那样实例化时不会传递这些情况。因此,只需检查选项或向其传递一个空对象。
相关文章:
- javascript中的数组出错
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- 使用javascript在Flash中加载外部图像
- 使用JavaScript检测Flash
- 在Meteor项目中安装并包含npm模块后出错
- Flash Uploadify在调用我的MVC控制器时没有保留会话/授权
- 解析JSON并从中删除对象会出错
- AngularJS指令出错-无法读取属性'编译'的未定义
- Flash AS3关闭浏览器窗口
- 使用javascript在MVC中查找网格长度时出错
- 尝试调用AngularJS上的函数时出错
- Google 脚本:用于创建日历活动的脚本运行时不会出错,但不会执行任何操作
- Expressjs/AngularJS:实现req-flash后出错
- req.flash()节点表达式4
- “TypeError: req.flash 不是一个函数”,使用带有 nodejs、用户名和密码身份验证的护照
- 如何将 req.flash() 与 EJS 一起使用
- 将 req.body 属性复制到猫鼬模型中时出错
- 无法同时控制台.log(req.flash(“error”))和呈现消息
- 使用字符集windows-1251处理req时出错
- Node.js/Express 出错时重复的 req.param 键