首页 > 解决方案 > NestJS - 无法解析队列?

问题描述

我正在关注文档以开始使用队列。我安装了@nestjs/bull, bull,@types/bull依赖项。这是我的app.module.ts

@Module({
    imports: [
        ConfigModule.forRoot({
            load: [configuration],
        }),
        BullModule.registerQueue({
            name: 'create_checkin',
            redis: {
                host: 'localhost',
                port: 6379,
            },
        }),
        EventModule,
    ],
})
export class AppModule {}

我在根模块中导入了 BullModule。这是我的event.service.ts

@Injectable()
export class EventService {

    constructor(
        @InjectQueue('create_checkin') private readonly createCheckinQueue: Queue,
    ) {
    }
}

当我启动服务器时,我收到以下错误消息:

Nest can't resolve dependencies of the EventService
Please make sure that the argument BullQueue_create_checkin at index [0] is available in the EventModule context.

我不知道我做错了哪一步。有人可以帮助我吗?

标签: nestjs

解决方案


BullModule有一个类似的问题,在我的情况下,将它添加到数组中就足够了exports,以便成功运行整个项目。像这样:

@Module({
  imports: [
    BullModule.registerQueue({ ... }),
    ...
  ],
  ...
  exports: [
    BullModule,  // <— this is important!
    ...
  ]
})

然后在我的服务中,我已经能够注入队列:

@InjectQueue('print') private queue: Queue<PrintJob>

推荐阅读