简体   繁体   English

我在从 NestJS 上已导入的模块导入服务时遇到问题

[英]I'm having trouble importing a service from an already imported module on NestJS

Alright, I'll try to make my problem as clear as possible.好的,我会尽量把我的问题说清楚。 My module structure is so far similar to this:到目前为止,我的模块结构与此类似:

OrderProductUnit contains the modules OrderProductUnitStatus and OrderProductUnitNotes. OrderProductUnit 包含模块 OrderProductUnitStatus 和 OrderProductUnitNotes。

From OrderProductUnitNotes' service, I want to import OrderProductUnitStatus' service.从 OrderProductUnitNotes 的服务中,我想导入 OrderProductUnitStatus 的服务。 I'm already importing OrderProductUnitStatus' repository from inside OrderProductUnitNotes' service and it works just fine.我已经从 OrderProductUnitNotes 的服务中导入了 OrderProductUnitStatus 的存储库,它工作得很好。 But when I try to import the service, NestJS throws me this error:但是当我尝试导入服务时,NestJS 会抛出这个错误:

Nest can't resolve dependencies of the OrderProductUnitNotesService (OrderProductUnitNoteRepository, OrderProductUnitStatusRepository, ?). Please make sure that the argument dependency at index [2] is available in the OrderProductUnitsModule context.

I've tried explicitly importing OrderProductUnitStatusService, but it doesn't seem to make a difference.我尝试过明确导入 OrderProductUnitStatusService,但似乎没有什么不同。 I've tried using the @Inject('OrderProductUnitStatusService') approach, but when I attempt this nest seems to hang and doesn't load all modules, making the API unresponsive as if it was completely down.我试过使用@Inject('OrderProductUnitStatusService') 方法,但是当我尝试这个嵌套似乎挂起并且没有加载所有模块时,使得 API 没有响应,就好像它完全关闭了一样。

I made the decision to group both the modules OrderProductUnitStatus and OrderProductUnitNotes inside the OrderProductUnit module because they're directly connected so it made perfect sense to me that both the Notes and Status be children of OrderProductUnit.我决定将 OrderProductUnitStatus 和 OrderProductUnitNotes 这两个模块分组到 OrderProductUnit 模块中,因为它们是直接连接的,所以对我来说,Notes 和 Status 都是 OrderProductUnit 的子级非常有意义。 But I'm starting to reconsider this decision, I'll be grateful if anyone can tell me if this approach was a good decision.但我开始重新考虑这个决定,如果有人能告诉我这种方法是否是一个好的决定,我将不胜感激。

At this point I don't really know what else to try so any suggestions are welcome.在这一点上,我真的不知道还能尝试什么,所以欢迎提出任何建议。 Also, let me know if any additional information is needed since the relation between the modules can be kinda messy since they're nested.另外,如果需要任何其他信息,请告诉我,因为模块之间的关系可能有点混乱,因为它们是嵌套的。

I answered this on Discord, but it's good to have the information out there, the error indicates a circular file import between your services.我在 Discord 上回答了这个问题,但最好有信息,错误表明您的服务之间存在循环文件导入。 When Nest mentions it cannot find dependency instead of an injection token's name (class name usually) it means that Typescript can't resolve the class properly.当 Nest 提到它找不到dependency项而不是注入令牌的名称(通常是类名)时,这意味着 Typescript 无法正确解析 class。 Removing this circular dependency is of course the best option, but if it isn't possible you can use @Inject(forwardRef(() => InjectedClass)) in the constructor to lazily set the injection token for Nest to resolve.移除这种循环依赖当然是最好的选择,但如果不可能的话,你可以在构造函数中使用@Inject(forwardRef(() => InjectedClass))来懒惰地设置注入令牌以供 Nest 解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM