在Nest JS中发出http请求时出现httpService未定义错误

时间:2020-06-23 10:54:55

标签: typescript axios nestjs

我正在尝试在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 {}

以下是我遇到的错误的屏幕截图 enter image description here

1 个答案:

答案 0 :(得分:2)

您使用@Injectable装饰器装饰所有使用Nest.js依赖注入功能的类

您可以在此处详细了解Nest.js https://docs.nestjs.com/providers

中依赖项注入的工作方式