Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 - how to import connect-mongo (session)?

In pre-ES6, this is how I import connect-mongo (session):

var MongoStore = require("connect-mongo")(session);

But how can I import it in ES6?

import MongoStore from 'connect-mongo';
let monStore = MongoStore(session);

Error:

const Store = connect.Store || connect.session.Store;
                                                  ^

TypeError: Cannot read property 'Store' of undefined

Any ideas?

like image 315
Run Avatar asked Dec 07 '22 21:12

Run


1 Answers

With connect-mongodb-session which is pretty similar, you can:

import { default as connectMongoDBSession} from 'connect-mongodb-session';

const MongoDBStore = connectMongoDBSession(session);

var store = new MongoDBStore({
  uri: 'mongodb://localhost:27017/tmp',
  collection: 'sessions'
});

Both packages (connect-mongodb-session and connect-mongo) are exporting an anonymous function that takes the express-session module as an argument. This function returns a constructor and is exported with modules.export, therefore it's considered a default export and you can import it with import { default as NameForAnonymousFunction } from 'connect-mongo'.

Now, I think that a good learning practice you should follow is to try to read the actual code of packages you are installing, at least the index.js.

  • connect-mongodb-session/index.js
  • [DEPRECATED] connect-mongo/index.js

Since connect-mongo is using [email protected] and connect-mongodb-session is using [email protected], i've added a deprecated tag before connect-mongo

like image 141
OpSocket Avatar answered Dec 11 '22 08:12

OpSocket