首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NestJ无法解决循环依赖项上的服务依赖关系

NestJ无法解决循环依赖项上的服务依赖关系
EN

Stack Overflow用户
提问于 2021-12-01 12:13:25
回答 1查看 1.8K关注 0票数 1

我的问题是在我的项目中有一个循环依赖关系。不幸的是,我不能用forwardRef解决这个问题。

以下结构:

OrderModule

  • OrderService
    • 我在orderService中有以下依赖项
    • PriceService
    • CustomerService
    • SalePriceService
    • ..。

PriceModule

  • PriceService
    • 我在priceService中有以下依赖项
    • OrderService
    • ProductService
    • ..。

我试过了官方文件中的所有选项。docs NestJ循环依赖关系

如果服务中有更多的依赖项,这里需要考虑什么?

非常感谢。诚挚的问候。

更新:

order.module.ts

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

order.service.ts

代码语言:javascript
复制
@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

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

price.service.ts

代码语言:javascript
复制
@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

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

product.service.ts

代码语言:javascript
复制
@Injectable()
export class ProductService extends GenericCrudService<ProductDocument> {
  constructor(
    @InjectModel(Product.name) readonly product: Model<ProductDocument>,
  ) {
    super(product);
  }
}

我得到的错误是:

代码语言:javascript
复制
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]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-03 16:02:37

这里有一个明显的循环依赖关系:OrdersModulePricesModule,然后返回,这个循环依赖是适当的forwardRef喂养的。然而,还有另一种,不太明显的循环依赖。OrdersModuleProductsModulePricesModule,因为下一个导入将是OrdersModule。因此,OrdersModule需要forwardRef ProductsModuleProductsModule需要forwardRef PricesModule。看起来这些服务本身并不是循环的,所以仅仅是模块需要前向引用。始终确保检查整个导入链,特别是当Nest试图报告类似于Scope [AppModule -> ProductModule -> PriceModule]的情况时。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70183995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档