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

标签 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]。如果我从 ApplicationModule 中删除 AgencyModule,NestJS 会成功编译,并且我可以进行 API 调用。

AgencyModule,
ApplicationModule,
AuthModule,
JobModule,
UserModule,

所有这些模块都是其服务提供商的其他模块所必需的,因此我没有使用 forwardRef() 在彼此之间导入它们,而是将它们设置为 Global() - May不是最佳实践,但是嘿嘿(它有效)。

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

最佳答案

使用@Global()不会自动解决循环依赖,你仍然需要在两边使用@Inject(forwardRef(() => MyService)),请参阅docs .


正如您自己所指出的,循环依赖 (forwardRef) 和全局模块 (@Global) 是不好的风格,应该避免。而是让你的依赖关系变得明确。如果遇到循环依赖,请提取双方导入的共享服务/模块中的常用部分,请参阅this thread .

关于javascript - Nest 无法解析 BlahService 的依赖关系,尽管它是 Global(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57561323/

相关文章:

javascript - 如何向状态 Hook 添加键?

javascript - 如何将WaveSurfer js导出为音频文件?

node.js - 使用 Mongo Node 驱动程序更新插入时,它是插入还是更新?

node.js - 使用 prismjs 生成静态 html - 如何启用行号?

javascript - 如何在 ReactJS 中通过对象数组过滤数组项?

javascript - D3 中的两个 csv http URL 数据相同的图表

node.js - 将 Mongoose 连接传递给模块

typescript - 类构造函数可选参数

typescript - NestJS 中的 CouchDB

javascript - javascript中的toString float