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;
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.
Don't pass HttpService in the providers. Import only HttpModule.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With