[SOLVED] Handle EventEmitterModule wildcards – NestJS

Issue

I don’t know how to handle the following emitter:

async create(createJobDto: CreateJobDto)  {
    this.eventEmitter.emit(
        ['job','create'],
        createJobDto,
    );
}

async update(createJobDto: CreateJobDto)  {
    this.eventEmitter.emit(
        ['job','update'],
        createJobDto,
    );
}

Listener:

@OnEvent('job.**')
handleJobEvent(data: CreateJobDto) {
    console.log(data);
});

The NestJS and the EventEmitter2 docs mention that I can use an array as a wildcard, but how can I distinguish between "create" and "update" in the listener? All that I seem to get in data is the job object.

P.S. I am correctly including the EventEmitterModule in my imports:

EventEmitterModule.forRoot({
  wildcard: true
}),

Solution

I think that’s not possible. You have to create two different listeners:

@OnEvent('job.create')
handleJobCreateEvent(data: CreateJobDto) {
    console.log(data);
});

@OnEvent('job.update')
handleJobUpdateEvent(data: CreateJobDto) {
    console.log(data);
});

Answered By – Terenoth

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *