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

我有一个 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是一个没有模块的共享服务。


温温酱
浏览 369回答 1
1回答

喵喔喔

Using@Global()不会自动解决循环依赖,你还是要@Inject(forwardRef(() => MyService))两边都用,看文档。正如您自己指出的,循环依赖 (&nbsp;forwardRef) 和全局模块 (&nbsp;@Global) 是糟糕的风格,应该避免。而是使您的依赖项明确。如果遇到循环依赖提取双方导入的共享服务/模块中的常用部分,请参阅此线程。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript