使用 ClientsModule.registerAsync() 时无法访问从共享模块提供的模块导出的服务

Cannot access to a service exported from a module that is provided by a shared module when using ClientsModule.registerAsync()

上下文是我创建了一个导入多个配置模块的共享配置模块,每个配置模块都导出自己的服务,共享模块导出每个导入的模块,目前在我的应用程序中我有应用程序模块导入共享配置模块和导入共享配置模块的 auth 模块。

在项目中,我正在使用 nestjs 微服务,我正在尝试使用 registerAsync 方法注册 ClientsModule 以访问我的 auth 模块中的 auth 配置服务。

目录结构:

/**
config/  
  - shared-config.module.ts
  - app/
     - app-config.service.ts
     - app-config.module.ts
  - auth/
     - auth-config.service.ts
     - auth-config.module.ts
  ... other config modules
- auth/
   - auth.module.ts
- app/
   - app.module.ts
*/

共享-config.module.ts :

@Module({
  imports: [AppConfigModule, MicroServiceAuthConfigModule, /* and other modules */],
  export: [AppConfigModule, MicroServiceAuthConfigModule, /* and other modules */]
})
export class SharedConfigModule {}

auth-config.module.ts :

@Module({
    imports: [
        ConfigModule.forRoot({
           ... some config
        }),
    ],
    providers: [MicroServiceAuthConfigService],
    exports: [MicroServiceAuthConfigService],
})
export class MicroServiceAuthConfigModule {}

问题是我正在尝试使用 MicroServiceAuthConfigService 在我的 AuthModule 中创建 ClientsModule

auth.module.ts :

@Module({
  imports: [
    SharedConfigModule,
    ClientsModule.registerAsync([
      {
        name: 'AUTH_PACKAGE',
        inject: [MicroServiceAuthConfigService],
        useFactory: (authConfigService: MicroServiceAuthConfigService) => ({
          transport: Transport.GRPC,
          options: {
            url: authConfigService.url,
            package: authConfigService.package,
            protoPath: authConfigService.protoPath,
          },
        }),
      },
    ]),
  ],
  controllers: [AuthController],
  providers: [AuthService],
})
export class AuthModule {}

app.module.ts :

@Module({
  imports: [AuthModule, SharedConfigModule],
})
export class AppModule {}

因为我已经导入了 SharedConfigModule,所以我应该访问 useFactory 中的 MicroServiceAuthConfigService 但我却遇到以下错误:

Nest can't resolve dependencies of the AUTH_PACKAGE (?). Please make sure that the argument MicroServiceAuthConfigService at index [0] is available in the ClientsModule context.

Potential solutions:

  • If MicroServiceAuthConfigService is a provider, is it part of the current ClientsModule?
  • If MicroServiceAuthConfigService is exported from a separate @Module, is that module imported within ClientsModule? @Module({ imports: [ /* the Module containing MicroServiceAuthConfigService */ ] })

奇怪的是,我在我的应用程序模块中注入了 SharedConfigModule,在我的文件 main.ts 中,我正在使用 app.get(MicroServiceAuthConfigService) 并且它正在工作。

所以我做错了什么?

在您的 ClientsModule.registerAsync 中,您需要添加 imports 其数组包含导出 MicroServiceAuthConfigService 提供程序的模块。看来你特别需要

@Module({
  imports: [
    SharedConfigModule,
    ClientsModule.registerAsync([
      {
        name: 'AUTH_PACKAGE',
        imports: [MicroServiceAuthConfigModule],
        inject: [MicroServiceAuthConfigService],
        useFactory: (authConfigService: MicroServiceAuthConfigService) => ({
          transport: Transport.GRPC,
          options: {
            url: authConfigService.url,
            package: authConfigService.package,
            protoPath: authConfigService.protoPath,
          },
        }),
      },
    ]),
  ],
  controllers: [AuthController],
  providers: [AuthService],
})
export class AuthModule {}