使用Axios的Nestjs [英] Nestjs using axios

查看:798
本文介绍了使用Axios的Nestjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个简单的演示有一个错误 https://docs.nestjs.com/techniques/http-module

This simple demo has an error https://docs.nestjs.com/techniques/http-module

import { Get, Controller, HttpService } from '@nestjs/common';
import { AxiosResponse } from 'axios'
import { Observable } from 'rxjs'
@Controller()
export class AppController {
  constructor(private readonly http: HttpService) {}
  @Get()
  root(): Observable<AxiosResponse<any>>  {
    return this.http.get('https://api.github.com/users/januwA');
  }
}

我该怎么办?

[Nest] 7356   - 2018-10-18 00:08:59   [ExceptionsHandler] Converting circular structure to JSON +9852ms
TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)


nest i
common version : 5.1.0
core version   : 5.1.0

推荐答案

您不能只返回整个AxiosResponse对象,因为它无法序列化为JSON.您最有可能希望获得这样的响应data:

You cannot just return the whole AxiosResponse object because it cannot be serialized to JSON. You most likely want to get the response data like this:

@Get()
root() {
  return this.http.get('https://api.github.com/users/januwA').pipe(
    map(response => response.data)
  );
}


或使用Promises:

@Get()
async root() {
  const response = await this.http.get('https://api.github.com/users/januwA').toPromise();
  return response.data;
}

这篇关于使用Axios的Nestjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆