nestjs/mongoose

Adding connectionName in forRoot and forFeature causes "Nest can't resolve dependencies of the CatsService" Error when running the sample 06-mongoose project from Nest

Closed this issue · 1 comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Latest versions of nestjs/mongoose (9.2.x and 9.1.x) module will throw an error of "Nest can't resolve dependencies of the CatsService" when importing the module with a connection name. This will essentially make multiple mongoDB database impossible to use. The minimum reproduction code is taken directly from the Nestjs repository under samples/06-mongoose, and the only modification I made was to add the connection name.

Reversing the module version back to 9.0.x will prevent the error from happening. Removing the connection name from app.module.ts and cats.module.ts will also eliminate the error.

In app.module.ts:

@Module({
  imports: [
    MongooseModule.forRoot('mongodb://localhost:27017/test', {connectionName: 'cats'}),  // the connection name is causing the issue                                                                                                     
    CatsModule,
  ],
})

In cats.module.ts

@Module({
  imports: [MongooseModule.forFeature([{ name: Cat.name, schema: CatSchema }], 'cats')], // the connection name 'cats' is causing the issue
  controllers: [CatsController],
  providers: [CatsService],
})

Minimum reproduction code

https://github.com/wanghe001/nestjs-mongoose-bug.git

Steps to reproduce

  1. npm i
  2. docker-compose up
  3. npm run start
  4. see error

Expected behavior

The application should be able to start after initialisation, which is what happens when removing the connection name or downgrade the module version to 9.0.x

Package version

9.2.0

mongoose version

6.4.6

NestJS version

9.0.1

Node.js version

16.13.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

#1437 (comment)

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.