#@Subscriber in Medusa-extender
27 messages · Page 1 of 1 (latest)
The command does not exists 👍
Even so, I love Medusa-extender 💜 Will have to create by hand 😌 Another question, but I can't fetch information directly from @Subscriber ?
`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?
For some reason, the subscription to Order.placed does not always work. There seems to be no problem with Redis, a bug in Medusa?
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?
I think that what you are describing seams weird, here is where the subscribers from the extender are registered https://github.com/adrien2p/medusa-extender/blob/main/src/loaders/subscribers.loader.ts
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 😂
Yes, an extremely simple file:
Jus having a break
`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?
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 ?
` 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);
}`
this one?
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
Yeah got it. Now I will try (this bug does not appear every time, sometimes everything is ok). I'll post the result.
`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 {}`
Tested. There seems to be no problem with this option.