上下文是我创建了一个共享的配置模块,该模块导入了多个配置模块,每个配置模块都在导出自己的服务,该共享模块导出了每个导入的模块,目前在我的应用程序中,我有一个应用程序模块可以导入共享的配置模块和导入共享配置模块的身份验证模块。
在该项目中,我正在使用nestjs微服务,并且尝试注册ClientsModule
withregisterAsync
方法来访问我的身份验证模块中的身份验证配置服务。
目录架构:
/**
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
*/
shared-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
创建ClientsModule
的我AuthModule
。
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,所以我应该访问中的MicroServiceAuthConfigService,useFactory
但是却出现以下错误:
Nest无法解析AUTH_PACKAGE(?)的依赖项。请确保在ClientsModule上下文中,索引为[0]的参数MicroServiceAuthConfigService可用。
潜在的解决方案:
- 如果MicroServiceAuthConfigService是提供程序,它是否是当前ClientsModule的一部分?
- 如果MicroServiceAuthConfigService是从单独的@Module导出的,那么该模块是否在ClientsModule中导入?@Module({imports:[/ *包含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 {}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句