我正在尝试像这样在另一个服务(订单)类中重用服务(库存)
order.module.ts
import { InventoryModule } from '../inventory/inventory.module';
@Module({
imports: [TypeOrmModule.forFeature([OrderEntity]), InventoryModule],
controllers: [OrderController],
providers: [OrderService],
})
export class OrderModule {}
,然后在InventoryService
内部使用OrderService
方法,
@Injectable()
export class OrderService {
constructor(
@InjectRepository(OrderEntity) private orderRepository: Repository<OrderEntity>,
private inventoryService: InventoryService) {}
/**
* Add order item
* @param data Object
*/
async add(data: OrderDTO) {
const { inventory_item_id } = data
const inventoryItem = await this.inventoryService.getOne(inventory_item_id)
if (inventoryItem.quantity_available <= 0) {
throw new Error('Cannot add item to order, insufficient quantities!')
}
const orderItem = await this.orderRepository.create(data)
if (orderItem) {
const updatedInventoryItemQuantity = inventoryItem.quantity_available - 1
const updatedInventoryItem = await this.inventoryService.edit(inventory_item_id, {
quantity_available: updatedInventoryItemQuantity,
})
}
await this.orderRepository.save(orderItem)
return orderItem
}
但是我得到以下堆栈跟踪:
Require stack:
- /Users/jred/workspace/projects/my-proj/dist/order/order.service.js
- /Users/jred/workspace/projects/my-proj/dist/order/order.controller.js
- /Users/jred/workspace/projects/my-proj/dist/order/order.module.js
- /Users/jred/workspace/projects/my-proj/dist/app.module.js
- /Users/jred/workspace/projects/my-proj/dist/main.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:623:15)
at Function.Module._load (internal/modules/cjs/loader.js:527:27)
at Module.require (internal/modules/cjs/loader.js:681:19)
at require (internal/modules/cjs/helpers.js:16:16)
at Object.<anonymous> (/Users/jred/workspace/projects/my-proj/dist/order/order.service.js:19:29)
at Module._compile (internal/modules/cjs/loader.js:774:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
at Module.load (internal/modules/cjs/loader.js:641:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:681:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/jred/workspace/projects/my-proj/dist/order/order.service.js',
'/Users/jred/workspace/projects/my-proj/dist/order/order.controller.js',
'/Users/jred/workspace/projects/my-proj/dist/order/order.module.js',
'/Users/jred/workspace/projects/my-proj/dist/app.module.js',
'/Users/jred/workspace/projects/my-proj/dist/main.js'
]
}
有人经历过吗?在另一个模块中调用导入服务的正确方法是什么?
答案 0 :(得分:0)
检查您的app.module.ts。
确保在imports数组中添加了InventoryModule。
此外,还要确保要从InventoryModule导出InventoryService。
// app.module.ts
@Module({
imports: [InventoryModule, ...othermodules],
exports: [],
providers: []
})
export class ApplicationModule {}
// inventory.module.ts
@Module({
imports: [...yourModules],
exports: [InventoryService],
providers: [InventoryService]
})
export class InventoryModule {}
答案 1 :(得分:0)
问题出在我的Order.service.ts
文件中,具有自动导入语法
此导入包含src
import { InventoryService } from 'src/inventory/inventory.service';
必须更改为此
import { InventoryService } from '../inventory/inventory.service';