Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix AXIOS_INSTANCE_TOKEN at index [0] is available in the Module context

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;
like image 866
techstack Avatar asked Aug 01 '20 17:08

techstack


2 Answers

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.

like image 188
Kartik Chauhan Avatar answered Oct 24 '22 11:10

Kartik Chauhan


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

like image 21
Nabin Kishore Sahoo Avatar answered Oct 24 '22 11:10

Nabin Kishore Sahoo