iMi*_*abi 0 typescript axios nestjs
我正在尝试在我的 Nest JS 应用程序中进行第三方 API 调用。由于 Nest JS 在底层使用 Axios,并且在其文档中拥有专门的页面https://docs.nestjs.com/techniques/http-module,因此我确保遵循文档中提供的实现,但我保留当我尝试通过 Nestjs 的 httpModule 发出 HTTP 请求时,遇到httpService undefined错误。我不确定我错过了什么,我尝试在这里搜索相关问题,但没有成功。请帮忙看一下,下面是我的代码示例。
银行验证.service.ts
import { HttpService } from '@nestjs/common';
import { Observable } from 'rxjs';
import { config } from 'dotenv';
import { AxiosResponse } from 'axios';
config();
export class BankVerificationService {
constructor(private httpService: HttpService){}
getBanks(countryCode): Observable<AxiosResponse<any>> {
console.log(this.httpService, '===============')
return this.httpService.get(`https://api.flutterwave.com/v3/banks/${countryCode}`, {
headers: {
Authorization: `Bearer ${process.env.FLUTTERWAVE_TEST_SECRET}`
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
下面是我的 Axios HTTP 模块配置
import { Module, HttpModule } from '@nestjs/common';
import { BankVerificationService } from '../payment/utils/bankVerification.service';
@Module({
imports: [HttpModule.register({
timeout: 3000,
})],
providers: [BankVerificationService],
})
export class ExternalHttpRequestModule {}
Run Code Online (Sandbox Code Playgroud)
小智 9
您可以使用使用 Nest.js 的依赖注入功能的 @Injectable 装饰器来装饰所有类
在这里您可以阅读有关 Nest.js 中依赖注入如何工作的更多信息https://docs.nestjs.com/providers
| 归档时间: |
|
| 查看次数: |
5628 次 |
| 最近记录: |