【发布时间】:2019-12-05 07:52:24
【问题描述】:
我遇到了一个问题。 我有这个示例存储库:https://github.com/pillowslept/nest-example
我想将RaceService注入HeroService,因为我想使用getById等一些方法。
我遵循此处描述的文档:https://docs.nestjs.com/modules
问题发生在我下一步的时候:
在RaceModule 我添加了RaceService 作为导出:
@Module({
imports: [TypeOrmModule.forFeature([RaceEntity])],
controllers: [RaceController],
providers: [RaceService],
exports: [RaceService],
})
在HeroModule我导入了RaceModule:
@Module({
imports: [RaceModule, TypeOrmModule.forFeature([HeroEntity])],
controllers: [HeroController],
providers: [HeroService],
})
在HeroService 我为RaceService 添加了注入:
constructor(
@InjectRepository(HeroEntity)
private heroRepository: Repository<HeroEntity>,
private readonly raceService: RaceService,
) {
}
控制台出现错误:
Error: Nest cannot create the module instance. Often, this is because of a circular dependency between modules. Use forwardRef() to avoid it
我尝试了不同的方法来导入服务,包括创建一个新的 CommonsModule,也将 forwardRef 放入 raceService 定义中,但出现另一个错误:
Nest can't resolve dependencies of the HeroService (HeroEntityRepository, ?). Please make sure that the argument at index [1] is available in the HeroModule context.
感谢您的帮助!
【问题讨论】:
-
我在您的 GitHub 存储库上提出了拉取请求,希望对您有所帮助。
-
@Juan 欢迎来到 SO!由于您的问题似乎已解决,请考虑将其添加为您自己问题的答案。其他人可能会从您的解决方案中受益,也可以看到您的问题已经解决。 :-)
标签: javascript node.js nestjs