NestJS - Can't resolve queue?

Viewed 3763

I am following doc to start to use queue. I installed @nestjs/bull, bull, @types/bull dependencies. And here is my app.module.ts:

@Module({
    imports: [
        ConfigModule.forRoot({
            load: [configuration],
        }),
        BullModule.registerQueue({
            name: 'create_checkin',
            redis: {
                host: 'localhost',
                port: 6379,
            },
        }),
        EventModule,
    ],
})
export class AppModule {}

I imported BullModule in root module. And here is my event.service.ts:

@Injectable()
export class EventService {

    constructor(
        @InjectQueue('create_checkin') private readonly createCheckinQueue: Queue,
    ) {
    }
}

And when I start server, I got the following error message:

Nest can't resolve dependencies of the EventService
Please make sure that the argument BullQueue_create_checkin at index [0] is available in the EventModule context.

I don't know which step I did wrong. Someone can help me?

4 Answers

Had a similar problem, in my case it was enough to add the BullModule to the exports array in order to successfully run the whole project. Like this:

@Module({
  imports: [
    BullModule.registerQueue({ ... }),
    ...
  ],
  ...
  exports: [
    BullModule,  // <— this is important!
    ...
  ]
})

Then in my service I've been able to inject the queue:

@InjectQueue('print') private queue: Queue<PrintJob>

Make sure you are placing EventService under providers array in EventModule.

@Module({
     providers: [EventService],
     controllers :[],
     imports: [YOUR_MODULES],
     exports: [EventService]
})
export class EventModule {}

Try importing BullModule straight in Event Module - I had the same problem and doing it this way make it work.

@Module({
  imports: [
    BullModule.registerQueueAsync({
      name: 'csv',
      useFactory: async (config: ConfigService) => ({
        redis: config.get('redis'),
      }),
      inject: [ConfigService],
    }),
  ],
  providers: [
    CsvService
  ],
  exports: [CsvService],
})
export class CsvModule {}

I know that it's async method, but maybe you should try.

Related