【发布时间】: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