question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InjectModel brake on mongoose 5.12.9 and higher

See original GitHub issue

Hi, I’m testing latest typegoose@beta.11 with mongoose 5.12.9 (tested also with 5.12.10 and 5.12.12) but the InjectModel got breaked.

constructor(
  @InjectModel(Account)
  private readonly accountModel: ReturnModelType<typeof Account>
) {
    super(accountModel);
    console.log(accountModel); //  Model { undefined }    => typegoose beta.10+ and mongoose 5.12.9+
                                   Model { Account }      => typegoose beta.9 and mongoose 5.12.8
}

I have tried several combination of typegoose and mongoose and I can say that the maximum supported versions are typegoose beta.9 and mongoose 5.12.8. InjectModel stop working from mongoose 5.12.9 and higher.

To be noted that from typegoose beta.10 the minimum supported version of mongoose is 5.12.9 so I can’t upgrade typegoose too. Thanks in advance

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
OoDeLallycommented, Sep 10, 2021

@GuillaumeGSO You did the same mistake as I did:


@Module({
  imports: [
    TypegooseModule.forRoot(config.mongoURI), CompanyDataModule, NotificationsModule],
  controllers: [
    AppController, // Should be there since it is AppModule's responsibility to instantiate this.
    NotificationsController // Should NOT be here since it is NOT AppModule's responsibility to instantiate this.
  ],
  providers: [
    AppService,// Should be there since it is AppModule's responsibility to instantiate this.
    CompanyDataService, // Should NOT be here since it is NOT AppModule's responsibility to instantiate this. However since this one doesnt have any deps, it doesnt crash. It will be instantiated twice though.
    NotificationService,  // Should NOT be here since it is NOT AppModule's responsibility to instantiate this.
    NotificationDataService // Should NOT be here since it is NOT AppModule's responsibility to instantiate this.
  ],
})
export class AppModule {}

Including services and controllers into the controllers and providers will make Nest try to instantiate them from that module (here AppModule, which should not instantiate them, but merely import them from NotificationModule). Only NotificationModule does have the required dependencies, and you only need to instanciate them from this module.

Remove the commented lines above, and your code will run.

1reaction
OoDeLallycommented, Sep 9, 2021

I had a similar problem, and my mistake was a combination of various bad dependencies. A - I was trying to export the model, instead of exporting the service. or B- I was exporting the service, but was including the service into the user’s providers (which leads to a double-instantiation). If you can create a minimal example, I can have a look, in case you made the same kind of mistakes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · kpfromer/nestjs-typegoose - GitHub
Mongoose 6.x Support Issue - no exported member 'ConnectionOptions'. #452 opened on Nov 3, ... InjectModel brake on mongoose 5.12.9 and higher.
Read more >
5.12.9 - mongoose - npm
mongoose. TypeScript icon, indicating that this package has built-in type declarations. 5.12.9 • Public • Published a year ago.
Read more >
nestjs-typegoose - Bountysource
Hi, I'm testing latest typegoose@beta.11 with mongoose 5.12.9 (tested also with 5.12.10 and 5.12.12 ) but the InjectModel got breaked.
Read more >
How to inject model if the model is in the root module only
Just do the MongooseModule.forFeature import in the same module as the forRoot import. :-) – Kim Kern. Dec 10, 2018 at 16: ...
Read more >
Mongoose v6.8.1: API docs
When nesting schemas, ( children in the example above), always declare the ... they have the potential to break Mongoose/ Mongoose plugins functionality....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found