尝试在 NestJS 中注入 Bull 队列时无法解决依赖关系

RRG*_*T19 6 nestjs bull-queue

我正在尝试使用 Bull/Redis 实现 Nodemailer 来处理 NestJS 中的电子邮件类型的任务。

\n

我有一个名为的共享模块EmailService,它将一个作业添加到我的队列中,为此,它需要注入Queuefrom \'bull\'

\n
\n

Nest 无法解析 EmailService 的依赖关系(?)。请确保索引 [0] 处的参数 BullQueue_mailqueue 在 SharedModule 上下文中可用。

\n
\n

我的结构

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n|  \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app.module.ts\n|  \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 config\n|  |  \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 nodemailer\n|  |     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 nodemailer.module.ts\n|  |     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 nodemailer.service.ts\n|  \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 modules\n|     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 modules that imports the SharedModule to send emails.\n|  \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 shared\n|     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 processors\n|     |  \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 email.processor.ts\n|     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 services\n|     |  \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 email\n|     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 shared.module.ts\n
Run Code Online (Sandbox Code Playgroud)\n

应用程序模块

\n
@Module({\n  imports: [\n    NodemailerModule,\n    // Al other modules (functionalities of my application that imports the SharedModule)\n  ],\n})\nexport class AppModule {}\n
Run Code Online (Sandbox Code Playgroud)\n

节点邮件模块

\n
@Module({\n  imports: [\n    MailerModule.forRootAsync({\n      useClass: NodemailerService,\n    }),\n    BullModule.registerQueueAsync({\n      useClass: NodemailerService,\n    }),\n  ],\n  exports: [NodemailerService, BullModule], // <- Exports BullModule\n  providers: [NodemailerService],\n})\nexport class NodemailerModule {}\n
Run Code Online (Sandbox Code Playgroud)\n

节点邮件服务

\n
@Injectable()\nexport class NodemailerService implements BullOptionsFactory {\n  constructor() {}\n\n  createBullOptions(): Promise<BullModuleOptions> | BullModuleOptions {\n    return {\n      name: \'myqueue\',\n      redis: {\n        host: \'localhost\',\n        port: 6379,\n      },\n    };\n  }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

现在这是我的 EmailService,它是 SharedModule 的一部分。

\n
import { Injectable } from \'@nestjs/common\';\nimport { InjectQueue } from \'@nestjs/bull\';\nimport { Queue } from \'bull\';\n\n@Injectable()\nexport class EmailService {\n\n  constructor(\n    @InjectQueue(\'myqueue\')\n    private readonly mailQueue: Queue,\n  ) {}\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

共享模块

\n
@Module({\n  imports: [NodemailerModule], // < imports this because it has the Bull configuration as we saw above\n  exports: [EmailService],\n  providers: [EmailService, EmailProcessor],\n})\nexport class SharedModule {}\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试按照以下步骤操作:

\n
    \n
  1. https://firxworx.com/blog/coding/nodejs/email-module-for-nestjs-with-bull-queue-and-the-nest-mailer/
  2. \n
  3. 在 Typescript 中实现 Bull 队列
  4. \n
\n

我不明白为什么我EmailService无法注入 BullQueue 依赖项。

\n

我在这里缺少什么?

\n

thi*_*ign 15

注入队列的模块需要导入队列注册。IE

folder/some.module.ts

@Module({
  imports: [
    BullModule.registerQueue({
      name: 'some-queue',
    }),
  ],
  providers: [SomeService],
})
export class SomeModule {}
Run Code Online (Sandbox Code Playgroud)

然后在SomeService

folder/some.service.ts

constructor(
  @InjectQueue('some-queue') private someQueue: Queue,
) {}
Run Code Online (Sandbox Code Playgroud)

  • 太棒了!我试图在另一个模块中注册队列,并将其导入到我的模块中,但它不起作用。谢谢哥们。 (2认同)

dan*_*ong 0

您需要在应用程序模块中导入 bullModule 。作为文档:为了防止BullConfigService在内部创建BullModule并使用从不同模块导入的提供程序,可以使用以下useExisting语法。

BullModule.forRootAsync({
  imports: [ConfigModule],
  useExisting: ConfigService,
});
Run Code Online (Sandbox Code Playgroud)

所以需要导入 BullModule 作为 root

查看文档:这里