Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 750
Souhail Mohamed Avatar asked Mar 16 '21 11:03

Souhail Mohamed


People also ask

Why is MongoDB not connecting?

However, the improper installation of client and problems with URI string may lead to MongoDB not connecting error. At Bobcares, we often get requests to fix MongoDB not connecting error as part of our Server Management Services. Today, we’ll see how our Support Engineers connect MongoDB via different methods and fix related errors.

How to reuse an existing mongoose connection?

There is a solution now in the updated Migration guide of mongo-connect: "To reuse an existing mongoose connection retreive the mongoDb driver from you mongoose connection using Connection.prototype.getClient () and pass it to the store in the client-option." the following is worked for me.

What is the mongo shell connection string example?

An example for Mongo shell with a connection string is given below. This describes host, domain name and port. mongo "mongodb://mongodb.example.com:27017/testdb?ssl=true"

What is MongoDB Compass?

1. MongoDB Compass MongoDB Compass is the graphical user interface for MongoDB. Also, it is available on Linux, Mac, or Windows. MongoDB Compass uses a simple point and click interface to create and modify rules that validate your data. In addition, we can easily identify a bottleneck or slow query that could cause performance problems.


5 Answers

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

like image 143
abernier Avatar answered Oct 26 '22 19:10

abernier


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
  })
}));
like image 32
Harish Nishad Avatar answered Sep 20 '22 07:09

Harish Nishad


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
        })
    })
);
like image 13
Bhaven Avatar answered Oct 26 '22 19:10

Bhaven


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

like image 5
Daniya Niazi Avatar answered Oct 26 '22 19:10

Daniya Niazi


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

like image 2
Souhail Mohamed Avatar answered Oct 26 '22 20:10

Souhail Mohamed