nestjs-telegram icon indicating copy to clipboard operation
nestjs-telegram copied to clipboard

Nest can't resolve dependencies of the TELEGRAM_MODULE_OPTIONS

Open sergeycherepanov opened this issue 2 years ago • 7 comments

Hi, when I follow steps from the readme I got following error:

ERROR [ExceptionHandler] Nest can't resolve dependencies of the TELEGRAM_MODULE_OPTIONS (?). 
Please make sure that the argument ConfigService at index [0] is available in the TelegramModule context.

sergeycherepanov avatar Apr 23 '22 01:04 sergeycherepanov

I got the same did any one solve this problem ?

trgkyle avatar Jul 01 '22 08:07 trgkyle

To use async module import like this:

TelegramModule.forRootAsync({
  useFactory: async (configService: ConfigService) => ({
    botKey: configService.get('BOT_TOKEN'),
  }),
  inject: [ConfigService],
}),

You need to make your config service globally visible, so in AppModule just add isGlobal: true

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
  ],
  providers: [AppService],
})
export class AppModule {}

embyth avatar Sep 10 '22 13:09 embyth

I tried this way and no more error:

@Injectable()
export class HookTelegramService {
  private readonly telegram: TelegramService

  async sendMessage(sendMessageParam: TelegramSendMessageParams): Promise<TelegramMessage> {
    const result = await this.telegram.sendMessage(sendMessageParam).toPromise()

    return result
  }
}

Move private readonly telegram: TelegramService outside constructor but this.telegram return undefined

anvu69 avatar Oct 05 '22 07:10 anvu69

Hi, when I follow steps from the readme I got following error:

ERROR [ExceptionHandler] Nest can't resolve dependencies of the TELEGRAM_MODULE_OPTIONS (?). 
Please make sure that the argument ConfigService at index [0] is available in the TelegramModule context.

I tried to use OnModuleInit and run perfect.

export class HookTelegramService implements OnModuleInit {
  private readonly logger = new Logger(HookTelegramService.name)

  private telegram: TelegramService

  constructor(private moduleRef: ModuleRef) {}

  onModuleInit() {
    this.telegram = this.moduleRef.get(TelegramService, { strict: false })
  }

  async sendMessage(sendMessageParam: TelegramSendMessageParams): Promise<TelegramMessage> {
    const result = await this.telegram.sendMessage(sendMessageParam).toPromise()

    return result
  }
}

anvu69 avatar Oct 05 '22 07:10 anvu69

you have to inject the service.

constructor(private readonly telegram: TelegramService) {}

julihermes avatar Jan 26 '23 20:01 julihermes

I removed the TelegramService from exports and providers and the error disappeared.

antonygiomarx avatar Feb 05 '23 09:02 antonygiomarx

you need to change your async import to include the ConfigModule:

TelegramModule.forRootAsync({ imports: [ConfigModule], useFactory: async (configService: ConfigService) => ({ botKey: configService.get('TELEGRAM_API_Key') }), inject: [ConfigService] }),

fromage9747 avatar Mar 07 '23 05:03 fromage9747