我正在尝试使用 Bull/Redis 实现 Nodemailer 来处理 NestJS 中的电子邮件类型的任务。
\n我有一个名为的共享模块EmailService,它将一个作业添加到我的队列中,为此,它需要注入Queuefrom \'bull\'。
\n\nNest 无法解析 EmailService 的依赖关系(?)。请确保索引 [0] 处的参数 BullQueue_mailqueue 在 SharedModule 上下文中可用。
\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\nRun 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 {}\nRun 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 {}\nRun 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}\nRun Code Online (Sandbox Code Playgroud)\n现在这是我的 EmailService,它是 SharedModule 的一部分。
\nimport { 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}\nRun 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 {}\nRun Code Online (Sandbox Code Playgroud)\n我尝试按照以下步骤操作:
\n我不明白为什么我EmailService无法注入 BullQueue 依赖项。
我在这里缺少什么?
\nthi*_*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)
您需要在应用程序模块中导入 bullModule 。作为文档:为了防止BullConfigService在内部创建BullModule并使用从不同模块导入的提供程序,可以使用以下useExisting语法。
BullModule.forRootAsync({
imports: [ConfigModule],
useExisting: ConfigService,
});
Run Code Online (Sandbox Code Playgroud)
所以需要导入 BullModule 作为 root
查看文档:这里
| 归档时间: |
|
| 查看次数: |
6872 次 |
| 最近记录: |