#@Subscriber in Medusa-extender

27 messages · Page 1 of 1 (latest)

obtuse zealot
#

I don't find in ./node_modules/.bin/medex g [option] command to generate @Subscriber.
Is it a mistake or is it meant to be?

proper shore
#

The command does not exists 👍

obtuse zealot
#

`constructor({ eventBusService }: { eventBusService: EventBusService }) {
this.eventBusService = eventBusService;
this.eventBusService.subscribe(
OrderService.Events.PLACED,
this.handleOrderCreation
);
}

private async handleOrderCreation(): Promise<void> {
// console.log("I have been called after a product has been placed.");
const response = await fetch(
"https://jsonplaceholder.typicode.com/posts/1"
);

const data = await response.json();
console.log("data",data);

}`

#

If i add console.log("test") after console.log("data",data);. Fetch works and everything is displayed in the console. And if you remove console.log("test); then nothing works. Why is that?

obtuse zealot
#

For some reason, the subscription to Order.placed does not always work. There seems to be no problem with Redis, a bug in Medusa?

obtuse zealot
#

Moreover, if I create via src/subscribers/orderNotifier.ts everything works without problems, and if via medusa extender, then it happens that the subscription to the order creation event does not pick up. Because of what it can be?

proper shore
#

It basically does nothing different from medusa. Build it from the container ^^ when you test it from src/subscribers or the module, it is exactly the same file?

#

I am on the motorway 😂

obtuse zealot
#

Yes, an extremely simple file:

proper shore
#

Jus having a break

obtuse zealot
#

`class OrderNotifierSubscriber {
constructor({ eventBusService }) {
eventBusService.subscribe("order.placed", this.handleOrder);
}
handleOrder = async (data) => {
const response = await fetch(
"https://jsonplaceholder.typicode.com/posts/1"
);
const post = await response.json();
console.log("data", post);
};
}

export default OrderNotifierSubscriber;`

#

and

#

`import { Subscriber } from "medusa-extender";
import {
OrderService,
EventBusService,
} from "@medusajs/medusa/dist/services";

@Subscriber()
export class OrderSubscriber {
private readonly eventBusService: EventBusService;

constructor({ eventBusService }: { eventBusService: EventBusService }) {
this.eventBusService = eventBusService;
this.eventBusService.subscribe(
OrderService.Events.PLACED,
this.handleOrderCreation
);
}

private async handleOrderCreation(data): Promise<void> {
const response = await fetch(
"https://jsonplaceholder.typicode.com/posts/1"
);

const post = await response.json();

console.log("data", post);

}
}`

#

and order.module.ts for Medusa-Extender

#

`import { Module } from "medusa-extender";
import { OrderSubscriber } from "./order.subscriber";

@Module({
imports: [OrderSubscriber],
})
export class OrderModule {}`

#

Is there a way to debug it somehow?

proper shore
#

I can see that both handlers in the two classes are not defined the same way. To debug you can put breakpoints

#

In the extender subscriber, can you copy past the handler from your src subscriber ?

obtuse zealot
#

this one?

proper shore
#

No, basically, copy past the class from the one you have in src and put it in place of the class you have in the module subscriber and add the decorator

obtuse zealot
#

`import { Subscriber } from "medusa-extender";
// import { OrderService, EventBusService } from "@medusajs/medusa/dist/services";

@Subscriber()
class OrderNotifierSubscriber {
constructor({ eventBusService }) {
eventBusService.subscribe("order.placed", this.handleOrder);
}
handleOrder = async (data) => {
const response = await fetch(
"https://jsonplaceholder.typicode.com/posts/1"
);
const post = await response.json();
console.log("data", post);
};
}
export default OrderNotifierSubscriber;`

#

`import { Module } from "medusa-extender";
import OrderNotifierSubscriber from "./order.subscriber";

@Module({
imports: [OrderNotifierSubscriber],
})
export class OrderModule {}`

obtuse zealot
#

Tested. There seems to be no problem with this option.