我在mongo中有一个数据库,用于存储名为“SessionStore”的会话状态,其中包含一个名为“Sessions”的集合来存储会话状态。
我正在使用表达式会话来管理会话。
设置会话的代码如下;
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
.
.
.
var sessionDb = 'SessionStore';
var sessionCollection = ['Sessions'];
var mongojs = require('mongojs');
var ObjectId = mongojs.ObjectId;
var dbSessions = mongojs(sessionDb, sessionCollection);
.
.
.
app.use(session({
secret: 'mysessionsecret',
name: 'connectionEngine',
store: MongoStore({db: dbSessions}),
proxy: true,
resave: true,
saveUninitialized: true
}));
除非到达最后一行,否则会失败并显示以下内容;
C:\Users\Paul\Documents\Visual Studio 2015\Projects\Connection-Engine\Connection-Engine\node_modules\connect-mongo\lib\c
onnect-mongo.js:126
self.emit(newState);
^
TypeError: undefined is not a function
at changeState (C:\Users\Paul\Documents\Visual Studio 2015\Projects\Connection-Engine\Connection-Engine\node_modules
\connect-mongo\lib\connect-mongo.js:126:12)
at MongoStore (C:\Users\Paul\Documents\Visual Studio 2015\Projects\Connection-Engine\Connection-Engine\node_modules\
connect-mongo\lib\connect-mongo.js:237:5)
at Object.<anonymous> (C:\Users\Paul\Documents\Visual Studio 2015\Projects\Connection-Engine\Connection-Engine\app.j
s:100:12)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.runMain [as _onTimeout] (module.js:501:10)
at Timer.listOnTimeout (timers.js:110:15)
我想我已经错过了部分设置,但我不确定是什么。任何人都可以提出建议。
答案 0 :(得分:0)
最后的调用不正确 - 并且需要创建一个MongoStore实例
app.use(session({
secret: 'w3happyf3ww3band0fbr0th3r5',
name: 'connectionEngine',
store: new MongoStore({db: 'SessionStore', collection: 'Sessions'}),
proxy: true,
resave: true,
saveUninitialized: true
}));
商店选项需要&#39; new&#39;在它之前。