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

Ale*_*erg 4 dependency-injection circular-dependency node.js nestjs

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

结构如下:

订单模块

  • 订单服务
    • 我在 orderService 中有以下依赖项
    • 价格服务
    • 客户服务
    • 销售价格服务
    • ...

价格模块

  • 价格服务
    • 我在 PriceService 中有以下依赖项
    • 订单服务
    • 产品服务
    • ...

我已经尝试了官方文档中的所有选项。 文档 NestJs 循环依赖

如果服务中有更多依赖项,这里必须考虑什么?

非常感谢。此致。

更新:

order.module.ts

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

订单.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);
  }
}
Run Code Online (Sandbox Code Playgroud)

价格.模块.ts

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

价格.服务.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);
  }
}
Run Code Online (Sandbox Code Playgroud)

产品模块.ts

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

产品.服务.ts

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

Run Code Online (Sandbox Code Playgroud)

我收到的错误是:

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]
Run Code Online (Sandbox Code Playgroud)

Jay*_*iel 5

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