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

Fixed issue with the new Mongoose 6 API not returning a MongoClient-Promise anymore #442

Closed
moritzlaube opened this issue Sep 21, 2021 · 3 comments
Labels

Comments

@moritzlaube
Copy link

I am submitting a solved issue to help out others who have a similar issue.

In my Node app I am using Mongoose 6 with express-session and connect-mongo.

I just upgraded to Mongoose 6 and I've had some issues passing the MongoClient(I want to use an existing connection to the database) to the clientPromise option of the MongoStore.create() function.

With the new Mongoose API connect-mongo kept on complaining that .then is not a function. It seems the getClient function on the mongoose.connection doesn't return a promise anymore.

Here is my fix:

/* CONNECT TO MONGODB */
mongoose.connect(process.env.MONGODB_CONNECTION_STRING)

/* FIX: CREATE A PROMISE FOR THE RETURNED MONGOCLIENT */
clientPromise = new Promise(function (resolve, reject) {
  resolve(mongoose.connection.getClient())
  reject(new Error('MongoClient Error'))
})

/* ... */

/* THEN USE IT TO CONNECT TO SESSION STORE */
app.use(
  session({
    secret: process.env.SESSION_SECRET,
    store: MongoStore.create({
      clientPromise,
    }),
  })
)
@JonasHavers
Copy link

Thanks for sharing, @moritzlaube !

You can also use the client option in ConnectMongoOptions:

const sessionStore = MongoStore.create({
  client: mongoose.connection.getClient(),
});

If you want to use the clientPromise option for whatever reason (e.g. promises chaining), there is also a new asPromise() function in Mongoose's Connection (docs) that returns "a promise that resolves when this connection successfully connects to MongoDB":

const clientPromise = mongoose.connection.asPromise().then((connection) => connection.getClient());

Or use a shorter version of @moritzlaube 's solution:

const clientPromise = Promise.resolve(mongoose.connection.getClient());

@moritzlaube
Copy link
Author

One addition: The above solution with client: mongoose.connection.getClient() did work perfectly when connecting to a local containerized mongo db instance. Yet, when trying to connect to MongoDB Atlas it doesn't work anymore.

Then, "promisifying" the client works:

const clientPromise = mongoose.connection.asPromise().then(connection => connection.getClient())

Don't know why. But connecting to Atlas works after this change.

@mingchuno mingchuno pinned this issue Feb 26, 2022
@stale
Copy link

stale bot commented Mar 2, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Mar 2, 2022
@stale stale bot closed this as completed Apr 16, 2022
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

2 participants