Below is my server.js file.I read documentation of connect-mongo and tried all but did not resolve my issue. I use mongodb compass and removed the connection code because they don't allowed me to insert extra code. Please help me
const app = express();
const MongoDbStore = require("connect-mongo");
// Session config
app.use(
session({
secret: process.env.COOKIE_SECRET,
resave: false,
store: MongoDbStore.create({
mongoUrl: process.env.MONGO_CONNECTION_URL,
}),
saveUninitialized: false,
cookie: { maxAge: 1000 * 60 * 60 * 24 },
})
);```
**Here is the error which I am facing**.
Assertion failed: You must provide either mongoUrl|clientPromise|client in options
C:\Users\Tahmeed Ullah\Desktop\realtime-pizza\node_modules\connect-mongo\build\main\lib\MongoStore.js:119
throw new Error('Cannot init client. Please provide correct options');
^
Error: Cannot init client. Please provide correct options
at new MongoStore (C:\Users\Tahmeed Ullah\Desktop\realtime-pizza\node_modules\connect-mongo\build\main\lib\MongoStore.js:119:19)
at Function.create (C:\Users\Tahmeed Ullah\Desktop\realtime-pizza\node_modules\connect-mongo\build\main\lib\MongoStore.js:136:16)
at Object.<anonymous> (C:\Users\Tahmeed Ullah\Desktop\realtime-pizza\server.js:37:25)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
**Note**
I have remove connect-mongo and then re installed it.
[1]: https://i.stack.imgur.com/AHAqb.png
CodePudding user response:
Try to add an collectionName to your MongoStore object:
const session = require("express-session");
const MongoStore = require("connect-mongo").default;
//session configuration
const mongoStore = MongoStore.create({
mongoUrl: url,
collectionName: "sessions",
});
app.use(
session({
secret: process.env.COOKIE_SECRET,
resave: false,
saveUninitialized: false,
store: mongoStore,
cookie: { maxAge: 1000 * 60 * 60 * 24 }, //cookie valid for 24 hours
})
);
where url is "mongodb://localhost/pizza"
Code from Stackoverflow
CodePudding user response:
const session = require("express-session"); const MongoStore = require("connect-mongo").default;
app.use( session({ secret: "thisismysecret", resave: false, saveUninitialized: false, store: MongoDbStore.create({ mongoUrl: "mongodb://localhost:27017/pizza", }), cookie: { maxAge: 1000 * 60 * 60 * 24 }, //cookie valid for 24 hours }) );