【发布时间】:2020-10-13 10:01:29
【问题描述】:
我正在尝试在我的 Nest JS 应用中进行第三方 API 调用。由于 Nest JS 在后台使用 Axios 并且在他们的文档https://docs.nestjs.com/techniques/http-module 中有一个专门的页面,我确保遵循文档中提供的实现,但我一直遇到 httpService undefined 错误当我尝试通过 Nestjs 的 httpModule 发出 HTTP 请求时。我不确定我错过了什么,我试图在这里搜索相关问题,但没有运气。请帮忙看一下,下面是我的代码示例。
bankVerification.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}`
}
});
}
}
下面是我为 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 {}
【问题讨论】:
-
似乎是 DI 问题。也许尝试将
@Injectable装饰器添加到服务中? -
天哪,我不敢相信我错过了。正如您所指出的,由于缺少 @Injectable 装饰器,这确实是一个依赖注入问题。非常感谢@MorKadosh 的回答。有效。请让它成为一个答案,让我接受它
标签: typescript axios nestjs