Get event name in Nestjs and EventEmitter2 handler
I have the following code:
@OnEvent("**")
public handleEverything(parentId: number): void {
// @ts-expect-error this.envent
console.log(this.event)
}
I tried to get the event name by using the this.event
but it returns undefined
. Is there a way to get the event name inside the handler using Nestjs and EventEmitter?
I think that is not possible. You can instead add a parameter to the payload when dispatching event and use it in the handler:
Dispatching:
this.eventEmitter.emit('eventName', { type: 'eventType', others: 'others' });
Listening:
@OnEvent("**")
public handleEverything(payload: any): void {
if (payload.type === 'eventType') {
// do something
}
}