【问题标题】:httpService undefined error when making http request in Nest JS在 Nest JS 中发出 http 请求时出现 httpService 未定义错误
【发布时间】: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


【解决方案1】:

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

在这里您可以阅读更多关于依赖注入在 Nest.js 中的工作原理https://docs.nestjs.com/providers

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-30
    • 2021-11-26
    • 1970-01-01
    • 2021-05-10
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    相关资源
    最近更新 更多