Medusa JS Service is not recognized On subscriber

225 Views Asked by At

Medusa's custom service is not referenced in the subscriber

Describe the bug I Have a service custom for example I referred on pre-existing example onboardingService when I run medusa develop i am able to see the response log while building it says

An error occurred while processing product-variant. updated: TypeError: Cannot read properties 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 Run on medusa start go to admin open any product update variant 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

Expecting to service injection works in subscriber event

1

There are 1 best solutions below

0
On

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