Can't resolve dependency when try to inject a Bull Queue in NestJS
Asked Answered
B

2

6

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?

Bandur answered 5/3, 2021 at 14:14 Comment(0)
Y
15

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,
) {}
Yellowknife answered 11/5, 2021 at 12:2 Comment(1)
Greate! I was trying to register the queue in another module, and import it in my module, but it doesn't work. Thanks buddy.Glamorous
F
0

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

Florettaflorette answered 6/3, 2021 at 12:40 Comment(3)
Still getting the issue. I'm not sure this is mandatory because I can declare de bull configuration in any other module, and just import that module in the app.module, just like I did. Take a loot at my nodemailer.module, it has the bull module configuration.Bandur
@Bandur that is mandatary. please read documentation and example in the end of page as i updated answerFlorettaflorette
I tried it but still getting the issue. I know that BullModule.forRoot is necessary, what I mean is that I can put that in other module and imported it in the app.module. Putting that out side for now, with your suggestion I received another error: Nest can't resolve dependencies of the BULL_CONFIG(default). I really don't know what is the issue or what is wrong.Bandur

© 2022 - 2024 — McMap. All rights reserved.