NestJS Queues中@OnQueueCompleted装饰器的result参数来源及为何始终为undefined的问题
result undefined in @OnQueueCompleted decorator in NestJS Queues? I’ve run into this exact issue before with BullMQ (the underlying queue library NestJS uses for its queue module), so let me break down why this is happening and how to fix it.
The Root Cause
By default, BullMQ does not store job results to save on Redis memory usage. That’s why even though you’re returning true from your @Process handler, the result isn’t persisted, so @OnQueueCompleted can’t access it.
Fixes to Get the Result Parameter Working
1. Enable Result Storage Globally for the Queue
When registering your queue module, add storeResult: true to the defaultJobOptions in your BullMQ configuration. This applies to all jobs in the queue:
@Module({ imports: [ BullModule.forRoot({ redis: { host: 'localhost', port: 6379, }, defaultJobOptions: { storeResult: true, // Enables result storage for all jobs in this queue }, }), BullModule.registerQueue({ name: 'myQueue', // Ensure this matches your queue's name }), ], }) export class AppModule {}
2. Enable Result Storage for Individual Jobs
If you don’t want to enable result storage globally, you can set it per job when adding it to the queue:
// In your service where you add the job async addMyProcessJob() { await this.myQueue.add('myProcess', { payload: 'your-data' }, { storeResult: true, // Only store the result for this specific job }); }
3. Ensure @OnQueueCompleted Listens to the Correct Queue
If you have multiple queues in your app, specify the queue name in the @OnQueueCompleted decorator to avoid listening to the wrong queue’s events:
@OnQueueCompleted({ name: 'myQueue' }) // Match your queue's registered name here async onCompleted(job: Job, result: any) { console.log(result); // Now this should log `true` as expected! }
Quick Verification Tip
After making these changes, you can check your Redis instance to confirm the job result is being stored. Look for keys starting with bull:<queue-name>:jobs:<job-id>:result — this confirms the result is persisted and available to the @OnQueueCompleted handler.
内容的提问来源于stack exchange,提问作者beingalex

