0

I have a database in mongo to store session state called 'SessionStore' with a collection called 'Sessions' to store session state.

I'm using expression-session for managing sessions.

Code to setup sessions is as follows;

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
}));

Except when it gets to the last line it fails with the following;

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)

I think I have missed some part of the set-up but I am not sure what. Can anyone suggest something.

4

1 回答 1

0

最终调用不正确 - 需要创建 MongoStore 的实例

app.use(session({
    secret: 'w3happyf3ww3band0fbr0th3r5',
    name: 'connectionEngine',
    store: new MongoStore({db: 'SessionStore', collection: 'Sessions'}),
    proxy: true,
    resave: true,
    saveUninitialized: true
}));

商店选项之前需要“新”。

于 2015-11-22T16:02:29.963 回答