【问题标题】:NestJs can't resolve service dependencies on a circular dependencyNestJs 无法解决循环依赖的服务依赖
【发布时间】:2021-12-04 00:02:37
【问题描述】:

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

如下结构:

订单模块

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

价格模块

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

我已经尝试了官方文档中的所有选项。 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]

【问题讨论】:

  • 您能详细说明为什么使用forwardRef 不能解决问题吗?
  • 在此处显示您的代码、模块和服务,对于了解正在发生的事情非常有帮助
  • 嗨 @JayMcDoniel 我刚刚添加了一个更新的问题,其中包含代码 sn-ps 和我遇到的错误。
  • 可以加ProductModule吗?
  • 嗨@JayMcDoniel,我刚刚添加了一个更新的问题并添加了产品模块和产品服务,最好的问候

标签: node.js dependency-injection nestjs circular-dependency


【解决方案1】:

所以这里有一个明显的循环依赖关系:OrdersModule 到 PricesModule 并返回,那个是正确的 forwardReffed。然而,还有另一个不那么明显的循环依赖。 OrdersModule 到 ProductsModule 到 PricesModule,因为下一个导入将是 OrdersModule。因此,OrdersModule 需要 forwardRef ProductsModule 和 ProductsModule 需要 forwardRef PricesModule。看起来服务本身不是循环的,所以它只是需要前向引用的模块。始终确保检查整个导入链,尤其是当 Nest 试图报告与 Scope [AppModule -&gt; ProductModule -&gt; PriceModule] 类似的情况时。

【讨论】:

  • 您好@JayMcDoniel,我知道这非常有效。感谢您的快速帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 2021-07-19
  • 1970-01-01
  • 2019-09-12
  • 2020-06-14
  • 2020-11-12
相关资源
最近更新 更多