简体   繁体   中英

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.

From OrderProductUnitNotes' service, I want to import OrderProductUnitStatus' service. I'm already importing OrderProductUnitStatus' repository from inside OrderProductUnitNotes' service and it works just fine. But when I try to import the service, NestJS throws me this error:

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. 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.

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. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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