30

I am using Axios in my project to call some third-party endpoints. I don't seem to understand the error

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

Potential solutions:
- If AXIOS_INSTANCE_TOKEN is a provider, is it part of the current TimeModule?
- If AXIOS_INSTANCE_TOKEN is exported from a separate @Module, is that module imported within TimeModule?
  @Module({
    imports: [ /* the Module containing AXIOS_INSTANCE_TOKEN */ ]
  })

This is the module

@Module({
  imports: [TerminalModule,],
  providers: [TimeService, HttpService],
  controllers: [TimeController]
})
export class TimeModule { }

This is the service

@Injectable()
export class TimeService {
    constructor(private httpService: HttpService,
        @InjectModel('PayMobileAirtime') private time: Model<Time>,       
        @Inject(REQUEST) private request: any,

    ) { }

This is an example of one of my get and post methods

 async PrimeAirtimeProductList(telcotime: string) {
        let auth = await this.TimeAuth()
        const productList = await this.httpService.get(`https://clients.time.com/api/top/info/${telcotime}`,
            {
                headers: {
                    'Authorization': `Bearer ${auth.token}`
                }
            }
        ).toPromise();

        return productList.data
    }

Post

const dataToken = await this.manageTimeAuth()
        const url = `https://clients.time.com/api/dataup/exec/${number}`

        const BuyTelcoData = await this.httpService.post(url, {
            "product_id": product_id,
            "denomination": amount,
            "customer_reference": reference_id
        }, {
            headers: {
                'Authorization': `Bearer ${dataToken.token}`
            }
        }).toPromise();

        const data = BuyTelcoData.data;
techstack
  • 1,367
  • 4
  • 30
  • 61

2 Answers2

57

Import HttpModule from @nestjs/common in TimeModule and add it to the imports array.

Remove HttpService from the providers array in TimeModule. You can directly import it in the TimeService.

import { HttpModule } from '@nestjs/common';
...

@Module({
    imports: [TerminalModule, HttpModule],
    providers: [TimeService],
    ...
})

TimeService:

import { HttpService } from '@nestjs/common';

If your response type is an Observable of type AxiosResponse, then import these two as well in the service file TimeService.

import { Observable } from 'rxjs';
import { AxiosResponse } from 'axios';

For reference, check out http-module and this post.

Kartik Chauhan
  • 2,779
  • 5
  • 28
  • 39
16

Don't pass HttpService in the providers. Import only HttpModule.

  • Thanks. It worked. But can you explain why it worked ? – Uzumaki Naruto Jul 07 '23 at 11:53
  • Because `imports` takes the list of modules that export the providers. By default the providers are encapsulated. Providers can be directly used in the services. Meaning HttpService can be directly used in the TimeService. Have a look at the documentation here [Nest Modules](https://docs.nestjs.com/modules) _This means that it's impossible to inject providers that are neither directly part of the current module nor exported from the imported modules. Thus, you may consider the exported providers from a module as the module's public interface, or API._ – sandiejat Jul 21 '23 at 03:34