NestJs 无法解决循环依赖的服务依赖

NestJs can't resolve service dependencies on a circular dependency

我的问题是我的项目有循环依赖。不幸的是我无法用 forwardRef 解决这个问题。

结构如下:

订单模块

价格模块

我已经尝试了官方文档中的所有选项。 docs NestJs circular-dependency

如果一个服务的依赖比较多,这里需要考虑什么?

非常感谢。最好的问候。

更新:

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

所以这里有明显的循环依赖:OrdersModulePricesModule 并返回,那个是正确的 forwardReffed。然而,还有另一种不那么明显的循环依赖。 OrdersModuleProductsModulePricesModule 因为下一次导入将是 OrdersModule。因此,OrdersModule 需要 forwardRef ProductsModuleProductsModule 需要 forwardRef PricesModule。看起来服务本身不是循环的,所以它只是需要前向引用的模块。始终确保检查整个导入链,尤其是当 Nest 尝试报告类似 Scope [AppModule -> ProductModule -> PriceModule].

的内容时