RRGT19
RRGT19

Reputation: 1677

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.

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'.

Nest can't resolve dependencies of the EmailService (?). Please make sure that the argument BullQueue_mailqueue at index [0] is available in the SharedModule context.

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

@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.

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/
  2. Implementing Bull Queue in Typescript

I cannot see why my EmailService cannot inject the BullQueue dependency.

What I am missing here?

Upvotes: 6

Views: 8898

Answers (2)

thisismydesign
thisismydesign

Reputation: 25152

The module that injects the queue needs to import the queue registration. I.e.

folder/some.module.ts

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

Then in SomeService

folder/some.service.ts

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

Upvotes: 16

danhuong
danhuong

Reputation: 212

you need to import bullModule in app module. 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.forRootAsync({
  imports: [ConfigModule],
  useExisting: ConfigService,
});

so import BullModule for root is neccessary

view documentation: here

Upvotes: 0

Related Questions