medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
26.25k stars 2.67k forks source link

Service is not recognized On subscriber #5417

Closed sanukhandev closed 1 year ago

sanukhandev commented 1 year ago

Bug report

Medusa's custom service is not referenced in the subscriber

Describe the bug

I Have service custom for example i referred on pre existing example onboardingService when i run medusa develop i m able to see response log while building it says

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

A clear and concise description of what the bug is.

System information

My Subscriber

//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;

script block 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 version (including plugins):7.1.1 Node.js version: 16.15.0 Database:postgres Operating system:windows (docker) Browser (if relevant):

Steps to reproduce the behavior

  1. Run on medusa start
  2. go to admin open any product
  3. update variant
  4. in console you will see

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

Expected behavior

it should show log testService

sanukhandev commented 1 year ago

Added Binding this.onProductCreated = this.onProductCreated.bind(this)

it worked