虽然Nest是Global(),但Nest无法解析BlahService的依赖项

时间:2019-08-19 17:19:52

标签: javascript node.js typescript dependency-injection nestjs

我在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中删除了ApplicationModule,则NestJS成功编译,并且可以进行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.

}

我的ApplicationModule文件。

@Global()
@Module({
  imports: [
    SharedModule,
    MongooseModule.forFeature([{ name: 'Application', schema: ApplicationSchema }]),
  ],
  controllers: [
    ApplicationController,
  ],
  providers: [
    ApplicationService,
    AwsService,
  ],
  exports: [
    ApplicationService,
  ],
})
export class ApplicationModule implements NestModule {
  public configure(consumer: MiddlewareConsumer) {
    consumer
      .apply()
      .forRoutes(
        { path: 'application', method: RequestMethod.GET },
      );
  }
}

我的ApplicationService文件。

@Injectable()
export class ApplicationService {
  constructor(
    @InjectModel('Application') private readonly applicationModel: Model<Application>,
    private readonly awsService: AwsService,
    private readonly userService: UserService,
    private readonly agencyService: AgencyService,
    private readonly jobService: JobService,
  ) {
    //
  }

  // More stuff here but not worth adding to the snippet.

}

AwsService是不带模块的共享服务。

1 个答案:

答案 0 :(得分:1)

使用@Global()不会自动解决循环依赖关系,您仍然必须在两边同时使用@Inject(forwardRef(() => MyService)),请参阅docs


正如您自己指出的那样,循环依赖项(forwardRef)和全局模块(@Global)的风格很差,应避免使用。而是使您的依赖关系明确。如果遇到循环依赖关系,请提取双方导入的共享服务/模块中的常用部分,请参见this thread