Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript User Authentication With Express and Mongo Improving the App with Custom Middleware Using MongoDB as a Session Store

matt thurmond
matt thurmond
8,743 Points

connect-mongo error: "Class constructor MongoStore cannot be invoked without 'new'"

This isn't a question, just a solution to the broken code in the "Using MongoDB as a Session Store" video.

I believe the code in the video is broken due to a new release of the connect-mongo package (v4.x).

My console error was: "Class constructor MongoStore cannot be invoked without 'new'"

To fix it, I updated the code to the following:

// require connect-mongo in app.js

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

// manage user sessions in app.js

app.use(session({
  secret: 'treehouse loves you', 
  resave: true, 
  saveUninitialized: false, 
  store: MongoStore.create({
    mongoUrl: 'mongodb://localhost:27017/bookworm'
  })
})); 

See here for more documentation of the latest connect-mongo release: https://github.com/jdesboeufs/connect-mongo

1 Answer

Leonardo Cavalcanti
Leonardo Cavalcanti
5,308 Points

I had the same problem. Congrats for fixing it!