mongodb-js/connect-mongodb-session

Await connection

MickL opened this issue · 0 comments

MickL commented

I noticed that when creating a new MongoDBStore new MongoDBStore({ ... }) the MongoDB connection gets established async and is not awaited. Following the example from the readme when server = app.listen(3000); is called, the MongoDB connection is probably not yet established and store.client is undefined.

In my opinion new MongoDBStore() should return a promise that can be awaited OR there should be a separate function for connecting:

const store = await new MongoDBStore({ ... });

OR:

const store = new MongoDBStore({ ... });
await store.connect();

Current workaround:

const store = new MongoDBStore({ ... });

await new Promise((resolve) => {
    store.on('connected', () => {
      resolve(undefined);
    });
  });