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

InjectModel brake on mongoose 5.12.9 and higher #424

Open
fasenderos opened this issue May 31, 2021 · 6 comments
Open

InjectModel brake on mongoose 5.12.9 and higher #424

fasenderos opened this issue May 31, 2021 · 6 comments

Comments

@fasenderos
Copy link

fasenderos commented May 31, 2021

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

@feliperoan
Copy link

Hello, could you solve it? I have the same error
Do you have a version that I can try in my package to solve the problem at least until there is a solution?

@fasenderos
Copy link
Author

I did not found a solution, so I have to stay with typegoose beta.9 and mongoose 5.12.8

@OoDeLally
Copy link

OoDeLally commented 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.

@GuillaumeGSO
Copy link

Hi, i have a similar problem on a very simple training project.
@OoDeLally : fell free to have a look on this branch : https://github.com/GuillaumeGSO/notifications-project/tree/adding_mongodb

I've been stucked on this for hours now....
My model is NotificationData but the error keep using NotificationDataModel
@InjectModel(NotificationData)

Error: Nest can't resolve dependencies of the NotificationDataService (?). Please make sure
that the argument NotificationDataModel at index [0] is available in the AppModule context.

@OoDeLally
Copy link

OoDeLally commented 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.

@GuillaumeGSO
Copy link

Thank you so much !
Just few lines and you saved my day !

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

No branches or pull requests

4 participants