如何在索引 [0] 处修复 AXIOS_INSTANCE_TOKEN 在模块上下文中可用
How to fix AXIOS_INSTANCE_TOKEN at index [0] is available in the Module context
我在我的项目中使用 Axios 来调用一些第三方端点。我似乎不明白
错误
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 */ ]
})
这是模块
@Module({
imports: [TerminalModule,],
providers: [TimeService, HttpService],
controllers: [TimeController]
})
export class TimeModule { }
这是服务
@Injectable()
export class TimeService {
constructor(private httpService: HttpService,
@InjectModel('PayMobileAirtime') private time: Model<Time>,
@Inject(REQUEST) private request: any,
) { }
这是我的 get 和 post 方法之一的示例
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;
从 @nestjs/common
导入 TimeModule
中的 HttpModule
并将其添加到 imports
数组。
从 TimeModule
中的 providers
数组中删除 HttpService
。你可以直接在TimeService
.
中导入
import { HttpModule } from '@nestjs/common';
...
@Module({
imports: [TerminalModule, HttpModule],
providers: [TimeService],
...
})
时间服务:
import { HttpService } from '@nestjs/common';
如果您的响应类型是类型 AxiosResponse
的 Observable
,则将这两个也导入服务文件 TimeService
.
import { Observable } from 'rxjs';
import { AxiosResponse } from 'axios';
参考 http-module and this 。
不要在提供程序中传递 HttpService。仅导入 HttpModule。
我在我的项目中使用 Axios 来调用一些第三方端点。我似乎不明白 错误
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 */ ]
})
这是模块
@Module({
imports: [TerminalModule,],
providers: [TimeService, HttpService],
controllers: [TimeController]
})
export class TimeModule { }
这是服务
@Injectable()
export class TimeService {
constructor(private httpService: HttpService,
@InjectModel('PayMobileAirtime') private time: Model<Time>,
@Inject(REQUEST) private request: any,
) { }
这是我的 get 和 post 方法之一的示例
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;
从 @nestjs/common
导入 TimeModule
中的 HttpModule
并将其添加到 imports
数组。
从 TimeModule
中的 providers
数组中删除 HttpService
。你可以直接在TimeService
.
import { HttpModule } from '@nestjs/common';
...
@Module({
imports: [TerminalModule, HttpModule],
providers: [TimeService],
...
})
时间服务:
import { HttpService } from '@nestjs/common';
如果您的响应类型是类型 AxiosResponse
的 Observable
,则将这两个也导入服务文件 TimeService
.
import { Observable } from 'rxjs';
import { AxiosResponse } from 'axios';
参考 http-module and this
不要在提供程序中传递 HttpService。仅导入 HttpModule。