'mongo-connect error with mongo-connect(session)

After searching around about it, I can't find any solution or mistake in my code about this error. I've got my app.js files inside my node JS application with the mongo-connect declaration :

const MongoStore = require('connect-mongo')(session)

And I've got this error :

TypeError: Class constructor MongoStore cannot be invoked without 'new' at Object. (/Users/souhailmohamed/project/devops/story website/app.js:11:20) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) at internal/main/run_main_module.js:17:47

There is my app.use code beloww :

app.use(session({
    secret: 'story book',
    resave: false,
    saveUninitialized: false,
    store: new MongoStore({ mongooseConnection: mongoose.connection })
}))

I understand pretty well about the

const MongoStore = require('connect-mongo')(session)

but I didn't understand the error. But it's from a tutorial from youtube by traversy media LinK



Solution 1:[1]

connect-mongo v4 introduces new signature (compared to v3).

Here is the official migration guide.


Also, here is a quick diff of what I changed in my specific project (using mongoose and dotenv) to upgrade from connect-mongo v3 to v4:

connect-mongo 3 to 4 diff

Solution 2:[2]

I had the same problem in my code and solved it using this:

const MongoDbStore = require('connect-mongo');

Remove (session) from the require statement and update the app.use() like this:

// Session Config
app.use(
    session({
        secret: 'story book',
        resave: false,
        saveUninitialized: false,
        store: MongoDbStore.create({
            mongoUrl: YourDatabaseURL
        })
    })
);

Solution 3:[3]

I was following the same so I discovered an npm pakage connect-mongodb-session. Install it and use the same code of session as Brad uses in his video

const MongoDBStore = require('connect-mongodb-session')(session);

//sessions
app.use(session({
    secret: 'keyboard cat',
    resave: false,
    saveUnitialized: false,
    store: new MongoDBStore({
        mongooseConnection: mongoose.connection
    })
    //cookie: { secure: true }
}))

const store = new MongoStore({
    uri: 'your mongo uri' ,
    collection:'mySessions'
})

//express middleware
app.use(session({
    secret: 'keyboard cat',
    resave: false,
    saveUninitialized: false,
    store: store

// Don't create a session until something is stored 
// cookie: { secure: true } - this wont work without https
}))

It solved my relogging issue also. If you close the tab and renter to localhost you get yourself logged in already

Solution 4:[4]

Ok so i've found a way to resolve it, i don't about the reason, on which version of express to use but i removed the (session) after mongo-connect and change the app.use to this :

app.use(session({
    secret: 'story book',
    resave: false,
    saveUninitialized: false,
    store: MongoStore.create({ mongoUrl: 'mongodb+srv://<id+ password>@cluster0.cq7f2.mongodb.net/DBname?retryWrites=true&w=majority' })
}))

Solution 5:[5]

Certain rule are changed for the syntax in "connect-mongo" document here

const MongoStore = require("connect-mongo");

  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: false,
  store: MongoStore.create({
    mongoUrl: process.env.MONGO_URI  //(URI FROM.env file)
  })
  //stroed it in database
}));

Solution 6:[6]

In your app.js make the following changes:

let MongoStore = require('connect-mongo');

app.use(
    session({
        secret: 'keyboard cat',
        resave: false,
        saveUninitialized: false,
        store: MongoStore.create({
            mongoUrl: 'your mongo url'
        })
    });
);

References:

Solution 7:[7]

I have same problem. Here is the solution:

var session = require('express-session');
var MongoStore = require('connect-mongo');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;


app.use(session({
  secret : 'mysecretkey',
  resave : true,
  saveUninitialized : true,
  store :MongoStore.create({ mongoUrl: DBUrl })
}));

Solution 8:[8]

app.use(session({
  store: MongoStore.create({ mongoUrl: 'mongodb://localhost/test-app' })
}));

// Advanced usage
app.use(session({
  store: MongoStore.create({
    mongoUrl: 'mongodb://user12345:foobar@localhost/test-app?authSource=admin&w=1',
    mongoOptions: advancedOptions // See below for details
  })
}));

Solution 9:[9]

Here is the solution:

const session = require('express-session');
const MongoStore = require('connect-mongo');
const mongoose = require('mongoose');
app.use(session({
    secret: 'story book',
    resave: false,
    saveUninitialized: false,
    store: new MongoStore({
      mongoUrl: mongoose.connection._connectionString,
      mongoOptions: {}
    })
}))

Solution 10:[10]

Just use the another npm package connect-mongodb-session and it should work https://www.npmjs.com/package/connect-mongodb-session

const MongoDBStore = require('connect-mongodb-session')(session);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Bhaven
Solution 3 anurag1905
Solution 4 Souhail Mohamed
Solution 5 Tilak Raj
Solution 6 Tyler2P
Solution 7 Saeed
Solution 8 Harish Nishad
Solution 9 AppTruck
Solution 10 Piyush Chandra