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.

Paul S Chapman
  • 832
  • 10
  • 37
  • store: MongoStore({db: dbSessions}) I think this is the problem you should have : store: new MongoStore({db: dbSessions}), – dege Nov 22 '15 at 16:04

1 Answers1

0

The final call is incorrect - and requires an instance of MongoStore to be created

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

The store option requires 'new' before it.

Paul S Chapman
  • 832
  • 10
  • 37