user11762401
user11762401

Reputation: 445

NestJS - Can't resolve queue?

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?

Upvotes: 8

Views: 8977

Answers (6)

MegaSpaceHamlet
MegaSpaceHamlet

Reputation: 371

You have to register the queue in event.module.ts, not app.module.ts.

EventService is a provider of EventModule, so it will have access only to objects imported into the EventModule.

// event.module.ts

@Module({
  imports: [BullModule.registerQueue({...})],
  providers: [EventService]
})

Upvotes: 1

hardikpchauhan
hardikpchauhan

Reputation: 1

You can do like below

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

Upvotes: 0

Hello Human
Hello Human

Reputation: 574

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>

Upvotes: 19

gdr2409
gdr2409

Reputation: 29

You have to import bull module inside the module you are trying to setup queue. You can also refer https://medium.com/@shikhar01.cse14/bull-queues-in-nestjs-306c51cb0ec2

Upvotes: 1

Jakub Kosior
Jakub Kosior

Reputation: 316

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.

Upvotes: 0

andressh11
andressh11

Reputation: 596

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

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

Upvotes: 0

Related Questions