You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NestJS Queues中@OnQueueCompleted装饰器的result参数来源及为何始终为undefined的问题

Why is 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 10:17:51