简体   繁体   English

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

[英]Can't resolve dependency when try to inject a Bull Queue in NestJS

I'm trying to implement Nodemailer with Bull/Redis to handle email-type of tasks in NestJS.我正在尝试使用 Bull/Redis 实现 Nodemailer 来处理 NestJS 中的电子邮件类型的任务。

I have a shared module called EmailService that add a job to my queue, to do this, it needs to inject the Queue from 'bull' .我有一个名为EmailService的共享模块,它向我的队列添加一个作业,为此,它需要从'bull'注入Queue

Nest can't resolve dependencies of the EmailService (?). Nest 无法解析 EmailService (?) 的依赖关系。 Please make sure that the argument BullQueue_mailqueue at index [0] is available in the SharedModule context.请确保索引 [0] 处的参数 BullQueue_mailqueue 在 SharedModule 上下文中可用。

My structure我的结构

├── src
|  ├── app.module.ts
|  ├── config
|  |  └── nodemailer
|  |     ├── nodemailer.module.ts
|  |     ├── nodemailer.service.ts
|  └── modules
|     ├── modules that imports the SharedModule to send emails.
|  └── shared
|     ├── processors
|     |  └── email.processor.ts
|     ├── services
|     |  └── email
|     ├── shared.module.ts

app.module应用程序模块

@Module({
  imports: [
    NodemailerModule,
    // Al other modules (functionalities of my application that imports the SharedModule)
  ],
})
export class AppModule {}

nodemailer.module nodemailer.module

@Module({
  imports: [
    MailerModule.forRootAsync({
      useClass: NodemailerService,
    }),
    BullModule.registerQueueAsync({
      useClass: NodemailerService,
    }),
  ],
  exports: [NodemailerService, BullModule], // <- Exports BullModule
  providers: [NodemailerService],
})
export class NodemailerModule {}

NodemailerService节点邮件服务

@Injectable()
export class NodemailerService implements BullOptionsFactory {
  constructor() {}

  createBullOptions(): Promise<BullModuleOptions> | BullModuleOptions {
    return {
      name: 'myqueue',
      redis: {
        host: 'localhost',
        port: 6379,
      },
    };
  }

}

Now this is my EmailService that is part of SharedModule.现在这是我的 EmailService,它是 SharedModule 的一部分。

import { Injectable } from '@nestjs/common';
import { InjectQueue } from '@nestjs/bull';
import { Queue } from 'bull';

@Injectable()
export class EmailService {

  constructor(
    @InjectQueue('myqueue')
    private readonly mailQueue: Queue,
  ) {}

}

SharedModule共享模块

@Module({
  imports: [NodemailerModule], // < imports this because it has the Bull configuration as we saw above
  exports: [EmailService],
  providers: [EmailService, EmailProcessor],
})
export class SharedModule {}

I have tried to follow the steps on:我试图按照以下步骤操作:

  1. https://firxworx.com/blog/coding/nodejs/email-module-for-nestjs-with-bull-queue-and-the-nest-mailer/ https://firxworx.com/blog/coding/nodejs/email-module-for-nestjs-with-bull-queue-and-the-nest-mailer/
  2. Implementing Bull Queue in Typescript 在 Typescript 中实现公牛队列

I cannot see why my EmailService cannot inject the BullQueue dependency.我不明白为什么我的EmailService不能注入 BullQueue 依赖项。

What I am missing here?我在这里缺少什么?

you need to import bullModule in app module.您需要在应用模块中导入 BullModule。 as documentation: In order to prevent the creation of BullConfigService inside BullModule and use a provider imported from a different module, you can use the useExisting syntax.作为文档:为了防止在BullModule BullConfigService使用从不同模块导入的提供程序,您可以使用useExisting语法。

BullModule.forRootAsync({
  imports: [ConfigModule],
  useExisting: ConfigService,
});

so import BullModule for root is neccessary所以为 root 导入 BullModule 是必要的

view documentation: here查看文档:这里

The module that injects the queue needs to import the queue registration.注入队列的模块需要导入队列注册。 Ie IE

folder/some.module.ts

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

Then in SomeService然后在SomeService

folder/some.service.ts

constructor(
  @InjectQueue('some-queue') private someQueue: Queue,
) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM