Search code examples
mongodbpassport.jsmongostore

Passport: fail to initialize MongoStore


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?


Solution

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