Nestjs - 导入服务未被其他服务识别

Posted

技术标签:

【中文标题】Nestjs - 导入服务未被其他服务识别【英文标题】:Nest.js - Imported service not recognized by another service 【发布时间】:2020-04-29 19:43:01 【问题描述】:

我正在尝试在另一个服务(订单)类中重用服务(库存),就像这样

order.module.ts

import  InventoryModule  from '../inventory/inventory.module';

@Module(
  imports: [TypeOrmModule.forFeature([OrderEntity]), InventoryModule],
  controllers: [OrderController],
  providers: [OrderService],
)
export class OrderModule 

然后像这样在OrderService 中使用InventoryService 方法

@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'
  ]

有人经历过吗?在另一个模块中调用导入服务的正确方法是什么?

【问题讨论】:

您是否从其模块中导出InventoryService 您遇到的是特定于 Nest 的错误,还是特定于 javascript 的错误?如果是 Nest 特定的,您收到的错误消息是什么? 这能解决您的问题吗? ***.com/a/51821523/4694994 【参考方案1】:

问题出在我的 Order.service.ts 文件中,使用自动导入语法

此导入包含src

import InventoryService from 'src/inventory/inventory.service';

不得不改成这个

import InventoryService from '../inventory/inventory.service';

【讨论】:

【参考方案2】:

检查您的 app.module.ts。

确保您在导入数组中添加了 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 

【讨论】:

以上是关于Nestjs - 导入服务未被其他服务识别的主要内容,如果未能解决你的问题,请参考以下文章

Apple 推送通知 - 提供给包裹的凭据未被识别

Flutter 中的 rxdart 使用未被识别

PushSharp - 提供给包的凭据未被识别

使用金额差来识别商品订单

如何识别来自不同租户nestjs多租户jwt的jwt令牌

FIRDataBaseReference 未被识别