简体   繁体   中英

NestJs can't resolve service dependencies on a circular dependency

i have the problem that i have a circular dependency in my project. Unfortunately I cannot solve this with forwardRef.

The following structure:

OrderModule

  • OrderService
    • I have the following dependencies in the orderService
    • PriceService
    • CustomerService
    • SalePriceService
    • ...

PriceModule

  • PriceService
    • I have the following dependencies in the priceService
    • OrderService
    • ProductService
    • ...

I've tried all the options from the Official Documentation. docs NestJs circular-dependency

What has to be considered here if there are more dependencies in a service?

Many thanks. Best regards.

Update:

order.module.ts

@Module({
  imports: [
    CustomerModule,
    ProductModule,
    MongooseModule.forFeature([{ name: 'Order', schema: OrderSchema }]),
    forwardRef(() => PriceModule),
  ],
  controllers: [OrderController],
  providers: [OrderService],
  exports: [OrderService],
})
export class OrderModule {}

order.service.ts

@Injectable()
export class OrderService extends GenericCrudService<OrderDocument> {
  constructor(
    @InjectModel(Order.name) readonly order: Model<OrderDocument>,
    private readonly productService: ProductService,
    @Inject(forwardRef(() => PriceService))
    private readonly priceService: PriceService,
  ) {
    super(order);
  }
}

price.module.ts

@Module({
  imports: [
    CustomerModule,
    SalePriceModule,
    MongooseModule.forFeature([{ name: 'Price', schema: PriceSchema }]),
    forwardRef(() => OrderModule),
  ],
  controllers: [],
  providers: [PriceService],
  exports: [PriceService],
})
export class PriceModule {}

price.service.ts

@Injectable()
export class PriceService extends GenericCrudService<PriceDocument> {
  constructor(
    @InjectModel(Price.name) readonly price: Model<PriceDocument>,
    private readonly customerService: CustomerService,
    private readonly salePriceService: SalePriceService,
    @Inject(forwardRef(() => OrderService))
    private readonly orderService: OrderService,
  ) {
    super(price);
  }
}

product.module.ts

@Module({
  imports: [
    PriceModule,
    MongooseModule.forFeature([{ name: 'Product', schema: ProductSchema }]),
  ],
  controllers: [ProductController],
  providers: [ProductService],
  exports: [ProductService],
})
export class ProductModule {}

product.service.ts

@Injectable()
export class ProductService extends GenericCrudService<ProductDocument> {
  constructor(
    @InjectModel(Product.name) readonly product: Model<ProductDocument>,
  ) {
    super(product);
  }
}

The error I'm getting is:

The module at index [1] of the OrderModule "imports" array is undefined.

Potential causes:
- A circular dependency between modules. Use forwardRef() to avoid it. Read more: https://docs.nestjs.com/fundamentals/circular-dependency
- The module at index [1] is of type "undefined". Check your import statements and the type of the module.

Scope [AppModule -> ProductModule -> PriceModule]
Error: Nest cannot create the OrderModule instance.
The module at index [1] of the OrderModule "imports" array is undefined.

Potential causes:
- A circular dependency between modules. Use forwardRef() to avoid it. Read more: https://docs.nestjs.com/fundamentals/circular-dependency
- The module at index [1] is of type "undefined". Check your import statements and the type of the module.

Scope [AppModule -> ProductModule -> PriceModule]

So there's the obvious circular dependency that you have here: OrdersModule to PricesModule and back, that one is properly forwardRef fed. However, there's another, not so obvious circular dependency too. OrdersModule to ProductsModule to PricesModule because the next import would be OrdersModule . Because of this, OrdersModule needs to forwardRef the ProductsModule and ProductsModule needs to forwardRef the PricesModule . It looks like the services themselves aren't circular, so it's just the modules that need the forward ref. Always make sure to check the entire import chain, especially when Nest is trying to report something like it did with Scope [AppModule -> ProductModule -> PriceModule] .

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