You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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,
}),
})
)
The text was updated successfully, but these errors were encountered:
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":
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.
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.
I am submitting a solved issue to help out others who have a similar issue.
In my Node app I am using
Mongoose 6
withexpress-session
andconnect-mongo
.I just upgraded to
Mongoose 6
and I've had some issues passing theMongoClient
(I want to use an existing connection to the database) to theclientPromise
option of theMongoStore.create()
function.With the new Mongoose API
connect-mongo
kept on complaining that.then is not a function
. It seems thegetClient
function on themongoose.connection
doesn't return a promise anymore.Here is my fix:
The text was updated successfully, but these errors were encountered: