我的问题是在我的项目中有一个循环依赖关系。不幸的是,我不能用forwardRef解决这个问题。
以下结构:
OrderModule
PriceModule
我试过了官方文件中的所有选项。docs NestJ循环依赖关系
如果服务中有更多的依赖项,这里需要考虑什么?
非常感谢。诚挚的问候。
更新:
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]发布于 2021-12-03 16:02:37
这里有一个明显的循环依赖关系:OrdersModule到PricesModule,然后返回,这个循环依赖是适当的forwardRef喂养的。然而,还有另一种,不太明显的循环依赖。OrdersModule到ProductsModule到PricesModule,因为下一个导入将是OrdersModule。因此,OrdersModule需要forwardRef ProductsModule,ProductsModule需要forwardRef PricesModule。看起来这些服务本身并不是循环的,所以仅仅是模块需要前向引用。始终确保检查整个导入链,特别是当Nest试图报告类似于Scope [AppModule -> ProductModule -> PriceModule]的情况时。
https://stackoverflow.com/questions/70183995
复制相似问题