You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Medusa订阅者自定义OnboardingService注入失败报错求助

Medusa订阅者生产环境服务注入失败问题

问题描述

在Medusa中自定义了OnboardingService,并在MyProductSubscriber中注入该服务以监听product:created事件。使用medusa develop开发模式时可正常输出日志,但运行medusa start生产模式后,更新商品变体时出现报错:

An error occurred while processing product-variant.updated: TypeError: Cannot read properties of undefined (reading 'onboardingService')

期望订阅者能正常注入OnboardingService,调用testService输出日志"testService"。

相关代码

订阅者代码

// Imports

type Injectables = {
  eventBusService: EventBusService;
  onboardingService: OnboardingService;
}

class MyProductSubscriber {
  private onboardingService: OnboardingService;

  constructor({ onboardingService, eventBusService }: Injectables) {
    this.onboardingService = onboardingService;
    eventBusService.subscribe('product:created', this.onProductCreated);
  }
  
  private onProductCreated = async ({id}) => {
    console.log('====================================');
    console.log('Product created', id);
    console.log('====================================');
    
    await this.onboardingService.testService({id})
  }
}

export default MyProductSubscriber;

OnboardingService代码

// imports

type InjectedDependencies = {
  manager: EntityManager;
  onboardingRepository: typeof OnboardingRepository;
};

class OnboardingService extends TransactionBaseService {
  protected onboardingRepository_: typeof OnboardingRepository;

  constructor({ onboardingRepository }: InjectedDependencies) {
    super(arguments[0]);
    this.onboardingRepository_ = onboardingRepository;
  }

  async retrieve(): Promise<OnboardingState | undefined> {
    const onboardingRepo = this.activeManager_.withRepository(
      this.onboardingRepository_
    );

    const status = await onboardingRepo.findOne({
      where: { id: Not(IsNull()) },
    });
    
    return status;
  }

  async testService({id}) {
    console.log("testService", id)
  }

  async update(data: UpdateOnboardingStateInput): Promise<OnboardingState> {
    return await this.atomicPhase_(
      async (transactionManager: EntityManager) => {
        const onboardingRepository = transactionManager.withRepository(
          this.onboardingRepository_
        );

        const status = await this.retrieve();
    
        for (const [key, value] of Object.entries(data)) {
          status[key] = value;
        }
    
        return await onboardingRepository.save(status);
      }
    );
  }
}

export default OnboardingService;

package.json脚本

"scripts": {
  "clean": "cross-env ./node_modules/.bin/rimraf dist",
  "build": "cross-env npm run clean && npm run build:server && npm run build:admin",
  "build:server": "cross-env npm run clean && tsc -p tsconfig.json",
  "build:admin": "cross-env medusa-admin build",
  "watch": "cross-env tsc --watch",
  "test": "cross-env jest",
  "seed": "cross-env medusa seed -f ./data/seed.json",
  "start": "npm run build && medusa start -p 9001",
  "start:custom": "cross-env npm run build && node --preserve-symlinks --trace-warnings index.js",
  "dev": "medusa develop -p 9001"
},

系统信息

  • Medusa版本(含插件):7.1.1
  • Node.js版本:16.15.0
  • 数据库:postgres
  • 操作系统:Windows(Docker)

复现步骤

  1. 运行medusa start
  2. 进入后台管理系统,打开任意商品
  3. 更新商品变体
  4. 控制台出现报错:An error occurred while processing product-variant.updated: TypeError: Cannot read properties of undefined (reading 'onboardingService')

预期行为

订阅者能正常注入OnboardingService,调用testService输出日志"testService"。


内容的提问来源于stack exchange,提问作者Sanu Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 02:47:05