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

Mongoose connection #397

Closed
adamhavel opened this issue Mar 3, 2021 · 4 comments
Closed

Mongoose connection #397

adamhavel opened this issue Mar 3, 2021 · 4 comments

Comments

@adamhavel
Copy link

I have an issue using Mongoose connection as shown in the provided example. I connect to MongoDB this way:

const { connection } = await mongoose.connect(`mongodb://mongo:${dbPort}/${dbName}`, {
     useUnifiedTopology: true,
     useNewUrlParser: true,
     useCreateIndex: true
});

When creating the session store, this doesn't work, because getClient doesn't return Promise but connect-mongo expects one:
clientPromise: connection.getClient()

Therefore, I have to do this:
clientPromise: Promise.resolve(connection.getClient()).

@mingchuno
Copy link
Collaborator

{
  // Consider this block scope, `connection` is not promise in here and show does `client`
  const { connection } = await mongoose.connect(`mongodb://mongo:${dbPort}/${dbName}`, {
       useUnifiedTopology: true,
       useNewUrlParser: true,
       useCreateIndex: true
  });
  const client = connection.getClient()
}
// if this block scope is inside an async function and what you get will be a promise

@adamhavel
Copy link
Author

adamhavel commented Mar 3, 2021

I'm not sure I understand what you are getting at. This is the official example:

const clientP = mongoose.connect(
  'mongodb://root:example@127.0.0.1:27017',
  { useNewUrlParser: true, useUnifiedTopology: true }
).then(m => m.connection.getClient())

The only reason the clientP in the example is a Promise is because it's returned by the then function which always wraps its return value in Promise.resolve. Because I'm not using Promises this way, but employ await instead, I don't get a Promise.

My point: clientPromise should internally wrap the given value with Promise.resolve and not make the user conform to one specific way of dealing with asynchrony.

@jackharrhy
Copy link

Is there an issue with clientPromise being Promise<MongoClient> | MongoClient | undefined instead of Promise<MongoClient> | undefined?

You can just as easily await mongoose.connect(... as you can mongoose.connect(...).then( it, therefore removing the need to ever be handling a Promise to a connection.

Because of clientPromise expecting a promise, I'll have to go the same route as @adamhavel and wrap it in a Promise.resolve.

@mingchuno
Copy link
Collaborator

Created 4.3.0 release: https://github.com/jdesboeufs/connect-mongo/blob/master/CHANGELOG.md#430---2021-03-08

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

No branches or pull requests

3 participants