[英]NestJs can't resolve service dependencies on a circular dependency
我的问题是我的项目中有循环依赖。 不幸的是,我无法用 forwardRef 解决这个问题。
以下结构:
订单模块
价格模块
我已经尝试了官方文档中的所有选项。 文档 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 {}
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);
}
}
价格.module.ts
@Module({
imports: [
CustomerModule,
SalePriceModule,
MongooseModule.forFeature([{ name: 'Price', schema: PriceSchema }]),
forwardRef(() => OrderModule),
],
controllers: [],
providers: [PriceService],
exports: [PriceService],
})
export class PriceModule {}
价格.服务.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);
}
}
产品.module.ts
@Module({
imports: [
PriceModule,
MongooseModule.forFeature([{ name: 'Product', schema: ProductSchema }]),
],
controllers: [ProductController],
providers: [ProductService],
exports: [ProductService],
})
export class ProductModule {}
产品.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]
所以这里有一个明显的循环依赖: OrdersModule
到PricesModule
并返回,那个是正确的forwardRef
馈送的。 然而,还有另一个不那么明显的循环依赖。 OrdersModule
到ProductsModule
到PricesModule
因为下一个导入将是OrdersModule
。 因此, OrdersModule
需要forwardRef
ProductsModule
并且ProductsModule
需要forwardRef
PricesModule
。 看起来服务本身不是循环的,所以它只是需要前向引用的模块。 始终确保检查整个导入链,尤其是当 Nest 试图报告与Scope [AppModule -> ProductModule -> PriceModule]
的情况时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.