17

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

Molda
  • 5,619
  • 2
  • 23
  • 39
Souhail Mohamed
  • 273
  • 1
  • 3
  • 10
  • What's on the line 11 in app.js? That's the line it complains about. – Molda Mar 16 '21 at 12:17
  • In line 11 i've : ```const MongoStore = require('connect-mongo')(session)``` – Souhail Mohamed Mar 16 '21 at 12:24
  • 1
    It looks like they changed what the module exports then. You might have different version then in the video. In docs it shows `const MongoStore = require('connect-mongo'); Mongostore.create(....);` nothing about `require('connect-mongo')(session)` – Molda Mar 16 '21 at 12:29
  • Ok i see, i'll check what's the new way to write it but i've already to do something like : ```const MongoStore = require('connect-mongo') const MongoStoreFinal = MongoStore(session) ``` and still won't work... – Souhail Mohamed Mar 16 '21 at 12:46

11 Answers11

20

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

abernier
  • 27,030
  • 20
  • 83
  • 114
12

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
        })
    })
);
Bhaven
  • 121
  • 3
5

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

catGPT
  • 417
  • 2
  • 11
Daniya Niazi
  • 180
  • 3
  • 10
2

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' })
}))

Souhail Mohamed
  • 273
  • 1
  • 3
  • 10
2

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
}));
Tilak Raj
  • 21
  • 3
2

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:

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
2

It seems that 'connect-mongo' is obsolete and the newer version includes 'connect-mongodb-session'

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

Use the above code instead and it should work without any errors.

Note: install the 'connect-mongodb-session' package using npm init connect-mongodb-session

1

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 })
}));
Saeed
  • 21
  • 2
1
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
  })
}));
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Tyler2P Aug 14 '21 at 19:18
0

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: {}
    })
}))
AppTruck
  • 21
  • 2
0

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);

Piyush Chandra
  • 159
  • 1
  • 4