Passport: fail to initialize MongoStore

76 Views Asked by At

I am trying to follow the example given here:

const mongoose = require("mongoose");
const session = require("express-session");
const MongoStore = require("connect-mongo")(session);

but this fails immediately with:

ServerMiddleware Error: Class constructor MongoStore cannot be invoked without 'new'

on the third line.

Is MongoStore documented somewhere? Is there a better example/tutorial I should following?

1

There are 1 best solutions below

0
Tom Slabbaert On BEST ANSWER

That tutorial is over 2 years old which contains deprecated syntax, you can either downgrade your npm pkg versions, or use the updated syntax:

import session from 'express-session'
import MongoStore from 'connect-mongo'


const app = express();
app.use(session({
  secret: 'foo',
  store: MongoStore.create(options)
}));