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

MongoStore.create({}) options 'client' and 'clientPromise' not compatible with Mongoose version 6 #436

Closed
bradenmitchell opened this issue Aug 28, 2021 · 9 comments
Labels

Comments

@bradenmitchell
Copy link

  • I'm submitting a ...
    [x] bug report
    [ ] feature request
    [ ] question about the decisions made in the repository
    [ ] question about how to use this project

  • Summary
    Mongoose has been updated to version 6. When using client or clientPromise in MongoStore.create({ }), sessions are no longer saved to database.

  • Other information (e.g. detailed explanation, stack traces, related issues, suggestions how to fix, links for us to have context, eg. StackOverflow, personal fork, etc.)

@rfox12
Copy link
Contributor

rfox12 commented Sep 9, 2021

Can you provide any information that this is a bug in connect-mongo and not mongoose? I don't use mongoose (I really don't see the advantages over the core mongodb driver); and I (among many others) have been using connect-mongo reliably in production.

@rfox12
Copy link
Contributor

rfox12 commented Sep 9, 2021

This will probably be solved with my PR that I'm trying to finish out

@SavarJ
Copy link

SavarJ commented Sep 16, 2021

Hi. Any update? Everyone I know uses mongoose for MongoDB and when I do
store: MongoStore.create({ client: mongoose.connection, dbName: "sessions", }
it gives me an error of TypeError: con.db is not a function

@mingchuno
Copy link
Collaborator

4.6.0 released

@mq1n
Copy link

mq1n commented Oct 21, 2021

It's still happening for me on 4.6.0

TypeError: con.db is not a function at ...\node_modules\connect-mongo\build\main\lib\MongoStore.js:126:18 at processTicksAndRejections (node:internal/process/task_queues:96:5)

@gkostov
Copy link

gkostov commented Feb 25, 2022

This example demonstrates how it's done with Mongoose 6 https://github.com/jdesboeufs/connect-mongo/blob/master/example/mongoose.js . Though I prefer a bit simpler approach (one that's also closer to what was there with previous versions):

...
store: MongoStore.create({
    client: mongoose.connection.getClient(),
...

@vishwajeetsinh-arham
Copy link

vishwajeetsinh-arham commented Dec 19, 2022

this works for me

const MongoStore = require('connect-mongo');

=================Main Code=============================

 const store = MongoStore.create({
    mongoUrl: dbUrl,
    secret: 'thisshouldbeabettersecret',
    touchAfter: 24 * 60 * 60
})

================= End Main Code=============================

store.on('error', function(e){
    console.log('Session Store Error', e)
})

const sessionConfig = {
    store,
    // change your default name
    name: 'session',
    secret: 'thisshouldbeabettersecret!', 
    resave: false, 
    saveUninitialized: true,
    cookie:{
        httpOnly: true,
        // don't use this on development it's break thing
        // secure: true,
        expires: Date.now() + 1000 * 60 * 60  * 24 * 7,
        maxAge: 1000 * 60 * 60  * 24 * 7
    }
}
app.use(session(sessionConfig))

@gkostov
Copy link

gkostov commented Mar 13, 2023

@vishwajeetsinh-arham , your code (the mongoUrl option) tells connect-mongo to create and use its own connection to Mongo and will not reuse an existing one (like one created with Mongoose). It may not be a big deal in most cases but, being constrained by available memory, I prefer to keep the number of connections to a minimum.

@rnher
Copy link

rnher commented May 18, 2023

Please using for mongo-store options { client : mongoose.connection.getClient() )

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

No branches or pull requests

8 participants