【问题标题】:NestJS - Can't resolve queue?NestJS - 无法解析队列?
【发布时间】:2020-03-13 09:10:27
【问题描述】:

我正在关注doc 开始使用队列。我安装了@nestjs/bullbull@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.

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

【问题讨论】:

  • 您在哪里提供了 EventService?你能提供更多你的代码吗?

标签: nestjs


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    您必须在您尝试设置队列的模块中导入公牛模块。也可以参考https://medium.com/@shikhar01.cse14/bull-queues-in-nestjs-306c51cb0ec2

    【讨论】:

    • 我试图在上面的代码中显示的相同:)
    • 谢谢,文章看起来不错,但是没有关于如何创建 ConfigModule 或 ConfigService 的参考,如果作者提供 GitHub 示例存储库就好了。
    【解决方案3】:

    确保将 EventService 放在 EventModule 中的 providers 数组下。

    @Module({
         providers: [EventService],
         controllers :[],
         imports: [YOUR_MODULES],
         exports: [EventService]
    })
    export class EventModule {}
    

    【讨论】:

      【解决方案4】:

      尝试直接在事件模块中导入 BullModule - 我遇到了同样的问题,并且这样做可以让它工作。

      @Module({
        imports: [
          BullModule.registerQueueAsync({
            name: 'csv',
            useFactory: async (config: ConfigService) => ({
              redis: config.get('redis'),
            }),
            inject: [ConfigService],
          }),
        ],
        providers: [
          CsvService
        ],
        exports: [CsvService],
      })
      export class CsvModule {}
      

      我知道这是异步方法,但也许你应该尝试一下。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-25
        • 2019-12-19
        • 2022-01-02
        相关资源
        最近更新 更多