为了账号安全,请及时绑定邮箱和手机立即绑定

Nest 无法解析 BlahService 的依赖关系,尽管它是 Global()

Nest 无法解析 BlahService 的依赖关系,尽管它是 Global()

温温酱 2021-09-17 10:23:24
我有一个 NestJS 问题,它似乎只适用于 1 个模块,其他所有模块都可以正常工作。我有以下模块。错误是:[ExceptionHandler] Nest can't resolve dependencies of the ApplicationService (ApplicationModel, AwsService, UserService, ?, JobService). Please make sure that the argument at index [3] is available in the ApplicationModule context.该AgencyService是[3]。如果我AgencyModule从ApplicationModuleNestJS 中删除成功编译,我可以进行 API 调用。AgencyModule,ApplicationModule,AuthModule,JobModule,UserModule,所有这些模块都是他们的服务提供商的其他模块所需要的,所以不要使用forwardRef()我刚刚制作的方法在彼此之间导入它们Global()- 可能不是最佳实践,但嘿嘿(它有效)。我的AppModule档案。@Module({  imports: [    MongooseModule.forRootAsync({      useFactory: (configService: ConfigService) => ({        uri: configService.get('MONGO_DB_URL'),        useNewUrlParser: true,      }),      imports: [ConfigModule],      inject: [ConfigService],    }),    ConfigModule,    AgencyModule,    ApplicationModule,    AuthModule,    DevModule,    JobModule,    UserModule,    VideoModule,  ],  controllers: [AppController],  providers: [AppService],})export class AppModule {}每个模块文件夹具有以下结构。agency/- dto/- agency.controller.ts- agency.interface.ts- agency.schema.ts- agency.service.ts- agency.module.ts我的AgencyModule档案。@Global()@Module({  imports: [    SharedModule,    MongooseModule.forFeature([{ name: 'Agency', schema: AgencySchema }]),  ],  controllers: [    AgencyController,  ],  providers: [    AgencyService,    AwsService,  ],  exports: [    AgencyService,  ],})export class AgencyModule implements NestModule {  public configure(consumer: MiddlewareConsumer) {    consumer      .apply()      .forRoutes(        { path: 'agency', method: RequestMethod.GET },      );  }}我的AgencyService档案。@Injectable()export class AgencyService {  constructor(    @InjectModel('Agency') private readonly agencyModel: Model<Agency>,    private readonly awsService: AwsService,    private readonly applicationService: ApplicationService,  ) {    //  }  // More stuff here but not worth adding to the snippet.}这AwsService是一个没有模块的共享服务。
查看完整描述

1 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

Using@Global()不会自动解决循环依赖,你还是要@Inject(forwardRef(() => MyService))两边都用,看文档


正如您自己指出的,循环依赖 ( forwardRef) 和全局模块 ( @Global) 是糟糕的风格,应该避免。而是使您的依赖项明确。如果遇到循环依赖提取双方导入的共享服务/模块中的常用部分,请参阅此线程


查看完整回答
反对 回复 2021-09-17
  • 1 回答
  • 0 关注
  • 368 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信