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

a few typescript errors after upgrading to 6.0.0 #10601

Closed
huineng opened this issue Aug 25, 2021 · 9 comments
Closed

a few typescript errors after upgrading to 6.0.0 #10601

huineng opened this issue Aug 25, 2021 · 9 comments
Labels
typescript Types or Types-test related issue / Pull Request

Comments

@huineng
Copy link

huineng commented Aug 25, 2021

I upgraded today to v 6 and had to clean up a few things

  1. I had to remove the useFindAndModify
await DataModel.findByIdAndUpdate({ _id: id }, update, {
        new: true,
        // -> removeuseFindAndModify: false,
    }).exec();

even the mongoose.set('useFindAndModify', false); does not work anymore

Argument of type '"useFindAndModify"' is not assignable to parameter of type 'keyof MongooseOptions'.ts(2345)

so i removed this option completely

  1. This find code is now giving a typescript error (which i as you can see omit as code still works, but not sure how to solve it
// @ts-ignore TS2589
        const docs: IJobLog[] = await JobLogModel.find(filter)
            .skip(body.first || 0)
            .limit(body.rows || 0)
            .sort({ jobstart: -1 })
            .lean()
            .exec();

error

Type instantiation is excessively deep and possibly infinite.ts(2589)
  1. i append a comment for an issue with connect-mongo ConnectMongoOption field client doesn't work with mongoose in TypeScript jdesboeufs/connect-mongo#433 but not sure which library should cover this
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

Everything is still working, i have no need to revert back but eventually i would like to get rid of my ts ignores

thanks

@sean-daley
Copy link

  1. The useFindAndModify change is documented in their migration guide at https://mongoosejs.com/docs/migrating_to_6.html

  2. Unfortunately, I don't have a suggestion on that one

  3. This is a total guess but are your dependencies pulling in multiple versions of mongodb? Mongoose 6 upgraded to mongodb 4+ ... the fact it's finding mongodb under /node_modules/mongoose as well as /node_modules/mongodb might seem to indicate that you're somehow getting multiple versions of mongodb (maybe something still depends on mongodb 3 as well?) and that's causing some issues somehow? Just a guess though. Something to look at.

@joniaiuser
Copy link

joniaiuser commented Aug 25, 2021

I'm seeing a new typescript error from v6 as well when using it with react-hook-forms.

Type error: Type of property '_id' circularly references itself in mapped type '{ [K in keyof ObjectId]-?: PathImpl<K & string, ObjectId[K]>; }'.

methods.setValue(`loanApplications.${selectedLoanApplicationIndex}._id` as "loanApplications.0._id", res._id);

@huineng
Copy link
Author

huineng commented Aug 25, 2021

thanks .. on

  1. my bad, i should have read it better, so i'm good

  2. thanks i think it has something to do with this https://github.com/mongodb/node-mongodb-native/releases/tag/v4.1.1 (generics in find). i also found some changes here e61def3
    Update : Never mind this , my typescript interface and mongoose schema were not aligned and now the error is gone

  3. could be indeed although this repo uses 4.1.1 while connect mongo uses 4.1.0 would be strange that a patch would make this difference, but i will ask them to upgrade to 4.1.1, i think the clients are now different

thanks

@vkarpov15
Copy link
Collaborator

  1. Glad you found the docs on this, but just to confirm, useFindAndModify is no longer necessary in 6.0.0. We've removed that flag and switched over to native findOneAndUpdate(), there's no way for us to support useFindAndModify because the MongoDB driver no longer has a findAndModify :)
  2. We'll look into this
  3. This is due to multiple versions of MongoDB. Note that you have both import("/node_modules/mongoose/node_modules/mongodb/mongodb").MongoClient and import("/node_modules/mongodb/mongodb").MongoClient . So you have two different versions of the MongoDB node driver installed, and you're trying to use a MongoClient instance from one version in a place where your code expects a MongoClient instance from a different version.

@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Aug 25, 2021
@vkarpov15 vkarpov15 added this to the 6.0.1 milestone Aug 25, 2021
@simllll
Copy link
Contributor

simllll commented Aug 25, 2021

I cannot find any documentaito abou tit, but it seems the return values of updateOne and co have changed?
Ther eis no nModified anymore, only modifiedCount? Is this correct? Also upserted is no upsertedCount?

@vkarpov15
Copy link
Collaborator

@simllll correct. Those are changes in v4.0.0 of the MongoDB node driver. We will add a note about those to our migration guide.

@simllll
Copy link
Contributor

simllll commented Aug 25, 2021

@simllll correct. Those are changes in v4.0.0 of the MongoDB node driver. We will add a note about those to our migration guide.

Ah thanks, if someone stumples upon this, update returns now somehting like this:
{
acknowledged: true,
modifiedCount: 0, // nModified
upsertedId: null,
upsertedCount: 0, // upserted
matchedCount: 0 // n
}

vkarpov15 added a commit that referenced this issue Aug 25, 2021
@vkarpov15
Copy link
Collaborator

@huineng regarding (2), the below script compiles fine, no issues. Can you please modify the below script to demonstrate the compiler failure you're seeing?

import { connect, model, Schema } from 'mongoose';

run().catch(err => console.log(err));

async function run() {
  await connect('mongodb://localhost:27017/test');

  interface IJobLog {
    testProp?: string;
  }
  const schema = new Schema<IJobLog>({ testProp: String });

  const JobLogModel = model<IJobLog>('Test', schema);

  const docs: IJobLog[] = await JobLogModel.find({})
    .skip(0)
    .limit(0)
    .sort({ jobstart: -1 })
    .lean()
    .exec();

  console.log(docs);
}

@huineng
Copy link
Author

huineng commented Aug 25, 2021

Thanks, i should have done my update separately and not editing my previous comment. But indeed it's working again. It even helped me to clean up the code a bit. It happened when my interface (in your case IJobLog) is not the same as the schema (i had a few more elements in my interface then in my schema). Aligning interface with schema fixed it.

Sorry for that. I'm all good. Just the issue with the different versions, but that will be solved 2

@vkarpov15 vkarpov15 removed this from the 6.0.1 milestone Aug 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

5 participants