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

ConnectMongoOption field client doesn't work with mongoose in TypeScript #433

Closed
riflowth opened this issue Aug 23, 2021 · 6 comments
Closed
Labels

Comments

@riflowth
Copy link

riflowth commented Aug 23, 2021

  • 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
    I have tried to get client connection from opening connection by call mongoose.connection.getClient() to use on client field of ConnectMongoOptions but my vscode give me an error.

import MongoStore from 'connect-mongo';
import mongoose from 'mongoose';

MongoStore.create({
  client: mongoose.connection.getClient(),
  collectionName: 'sessions',
  ttl: 14 * 24 * 60 * 60,
  autoRemove: 'native'
});
  • 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.)

My vscode show this error

Type 'MongoClient' is missing the following properties from type 
'MongoClient': options, serverApi, autoEncrypter, readConcern, and 3 more.
@pararell
Copy link

pararell commented Aug 25, 2021

There is a problem with type Promise<MongoClient> for the clientPromise, it is in the same code as example in typescript:
https://github.com/jdesboeufs/connect-mongo/blob/master/example/mongoose.js
It would be useful to add all examples to typescript, that should prevent these issues.
Now here I have to use any to prevent the issue:

  const clientP: any = mongoose.connect(process.env.MONGO_URI, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
  }).then(m => m.connection.getClient());

@huineng
Copy link

huineng commented Aug 25, 2021

i got the above working since a long time without any issue. But today mongoose release v 6.0.0 and now i have an incompatibility issue

store: MongoStore.create({
            client: mongoose.connection.getClient(),
            stringify: false,
}),
Type 'import("/node_modules/mongoose/node_modules/mongodb/mongodb").MongoClient' is not 
assignable to type 'import("/node_modules/mongodb/mongodb").MongoClient'.
  The types of 'options.autoEncrypter' are incompatible between these types.
    Type 'import("/node_modules/mongoose/node_modules/mongodb/mongodb").AutoEncrypter | undefined' is 
not assignable to type 'import("/node_modules/mongodb/mongodb").AutoEncrypter | undefined'.ts(2322)
MongoStore.d.ts(15, 5): The expected type comes from property 'client' which is declared here 
on type 'ConnectMongoOptions'
(property) client?: MongoClient | undefined

and i'm not sure if these needs to be solved by mongoose or by connect-mongo

i'm ignoring it for now as i did not see any differences and all still works

store: MongoStore.create({
            // @ts-ignore TS2322
            client: mongoose.connection.getClient(),
            stringify: false,
        }),

@huineng
Copy link

huineng commented Aug 25, 2021

Is it possible to update mongodb to 4.1.1. there are differences between the 2 packages https://github.com/mongodb/node-mongodb-native/releases/tag/v4.1.1. thanks

@mingchuno
Copy link
Collaborator

Hi guys. Sorry for the late reply. Does moving mongodb to peerDependency help solve the issue?
Link to #434

@mingchuno
Copy link
Collaborator

4.6.0 released

@MaxMansfield
Copy link

MaxMansfield commented Apr 3, 2022

@mingchuno I'm still seeing this issue with MongoDB set as a peerDep. I removed and reinstalled npm packages too.

"mongoose": "^6.2.3"
"connect-mongo": "^4.6.0"

"mongodb": "^4.4.1"

Any thoughts?

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

5 participants