gpt4 book ai didi

javascript - Expressjs/AngularJS : Error after implementing req-flash

转载 作者:太空宇宙 更新时间:2023-11-04 00:40:01 25 4
gpt4 key购买 nike

我像这样实现了 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 - Expressjs/AngularJS : Error after implementing req-flash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37254646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com