Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDBStore session broke, needs a clear fix #14

Open
Markalot787 opened this issue Sep 20, 2021 · 12 comments
Open

MongoDBStore session broke, needs a clear fix #14

Markalot787 opened this issue Sep 20, 2021 · 12 comments

Comments

@Markalot787
Copy link

const MongoDBStore = require("connect-mongo")(session);

const secret = process.env.SECRET || 'thisshouldbeabettersecret!';

const store = new MongoDBStore({
url: dbUrl,
secret,
touchAfter: 24 * 60 * 60
});

store.on("error", function (e) {
console.log("SESSION STORE ERROR", e)
})

const sessionConfig = {
store,
name: 'session',
secret,
resave: false,
saveUninitialized: true,
cookie: {
httpOnly: true,
// secure: true,
expires: Date.now() + 1000 * 60 * 60 * 24 * 7,
maxAge: 1000 * 60 * 60 * 24 * 7
}
}

app.use(session(sessionConfig));

@Markalot787
Copy link
Author

Markalot787 commented Sep 20, 2021

//replaced with this solution i found but still broke

const mongoStore = require("connect-mongo");
const secret = process.env.SECRET || 'thisshouldbeabettersecret';

const sessionConfig = {
name: 'session',
secret,
resave: false,
saveUninitialized: true,
store: mongoStore.create({
mongoUrl: dbUrl,
secret: secret,
touchAfter: 24 * 60 * 60,
}),
cookie: {
httpOnly: true,
expires: Date.now()+ 1000 * 60 * 60 * 24 * 7,
maxAge: 1000 * 60 * 60 * 24 * 7
}
}

sessionConfig.store.on("error", function (e) {
console.log("SESSION STORE ERROR", e)
});

app.use(session(sessionConfig))

@nax3t
Copy link
Contributor

nax3t commented Sep 20, 2021

What's the error that you're getting?

@Markalot787
Copy link
Author

i ended up with this

const secret = process.env.SECRET || 'thisshouldbeabettersecret';

const store = mongoStore.create({
mongoUrl: dbUrl,
secret,
touchAfter: 24 * 60 * 60

});

// store.on("error", function (e) {
// console.log("SESSION STORE ERROR", e)
// })

const sessionConfig = {
store,
name: 'session',
secret,
resave: false,
saveUninitialized: true,
cookie: {
httpOnly: true,
// secure: true,
expires: Date.now() + 1000 * 60 * 60 * 24 * 7,
maxAge: 1000 * 60 * 60 * 24 * 7
}
}

// sessionConfig.store.on("error", function (e) {
// console.log("SESSION STORE ERROR", e)
// });

app.use(session(sessionConfig))

@Markalot787
Copy link
Author

const store = mongoStore.create({
^

TypeError: mongoStore.create is not a function
at Object. (D:\apps\YelpCamp-master2\app.js:54:26)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! yelpcamp@1.0.0 start: node app.js
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the yelpcamp@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

@Abdulrasheedar
Copy link

DId you find the solution for this ?? @Markalot787

@prince1911
Copy link

Assertion failed: You must provide either mongoUrl|clientPromise|client in options
C:\Users\Prince\YelpCamp\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\Prince\YelpCamp\node_modules\connect-mongo\build\main\lib\MongoStore.js:119:19)
at Object. (C:\Users\Prince\YelpCamp\app.js:51:13)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47

@U-4D89
Copy link

U-4D89 commented Nov 10, 2021

Same issue here, I installed the same version that Colt uses in the videos.
npm i connect-mongo@^3.2.0

@nax3t
Copy link
Contributor

nax3t commented Nov 23, 2021

please see here

@benilrichards
Copy link

const MongoDBStore = require("connect-mongo")  // change this line

const store = MongoDBStore.create({  // change this line
	mongoUrl: dbUrl,  // change this line
	secret,
	touchAfter: 24 * 60 * 60,
});

This worked for me.

My installed dependency version
"connect-mongo": "^4.6.0"

@naeemishere
Copy link

@benilrichards Thanks for sharing this solution, I had similar issue in console "Error: Cannot init client. Please provide correct options", I'm using ("^4.6.0"). Although variable name i used is "MongoStore" instead of "MongoDBStore" and it still worked!

const store = new MongoStore({
    mongoUrl: dbUrl,
    secret: 'thisshouldbeabettersecret!',
    touchAfter: 24 * 60 * 60
}); 

@I-karmayogi
Copy link

I-karmayogi commented May 28, 2023

still showing me the error with your above code
Any idea with new versions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants