Null store object with connect-mongo and express

I'm using Express and Connect-Mongo to manage sessions for my Node.js/Socket.io app.
I looked at the connect-mongo doc and I'm trying to initialize the DB connection but whenever I try to store anything in it the store object is always null and crashes.
Here's a snippet of the code I'm using:

var mongoserver = new mongodb.Server('localhost', mongodb.Connection.DEFAULT_PORT, {safe:true}) ;
var store ;
app.use(express.session({
secret: 'secret', 
store: new MongoStore({ db: 'test'}, function(){
    db = new mongodb.Db('test', mongoserver, { w: 1 }) ;
    db.open(function(err, db){ 
        if(err){console.log('DB error', err) ;}

        db.collection('test', function(err, collection) {
          console.log('Collection...') ;
        });

        db.on("close", function(error){
            console.log("Connection to the database was closed!");
        });
    });
})
}));
io.sockets.on('connection', function (socket) {
store.set('test-sid', {foo:'bar'}, function(err, session) {}) ;
)};

All other variables (express, server etc) are properly initalized, the app comes up, I see in the console that the connection to mongo is accepted. Any ideas ??

You are not storing the store in the variable "store". It is being passed into the session configuration and set as an express application setting.

To fix this you can create the store and assign it to the variable before adding it as a setting and then pass it into the session config:

// Create the store
var store = new MongoStore(...);

// Pass into session creation
app.set(expression.session({
    store: store
});

// You can now access the store from the variable as you expected
store.set('test-sid', {foo: 'bar'}, function(err, session) {});