Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用axios的Nestjs_Javascript_Node.js_Axios_Nestjs - Fatal编程技术网

Javascript 使用axios的Nestjs

Javascript 使用axios的Nestjs,javascript,node.js,axios,nestjs,Javascript,Node.js,Axios,Nestjs,这个简单的演示有一个错误 在示例中编写时,get方法返回AxiosResponse,并包含循环引用。 因此,如果您想代理webservicehttps://api.github.com/users/januwA,您应该返回: 从'@nestjs/common'导入{Get,Controller,HttpService}; 从“axios”导入{AxiosResponse} 从“rxjs”导入{observatable} @控制器() 导出类AppController{ 构造函数(私有只读htt

这个简单的演示有一个错误


在示例中编写时,
get
方法返回
AxiosResponse
,并包含循环引用。 因此,如果您想代理webservice
https://api.github.com/users/januwA
,您应该返回:

从'@nestjs/common'导入{Get,Controller,HttpService};
从“axios”导入{AxiosResponse}
从“rxjs”导入{observatable}
@控制器()
导出类AppController{
构造函数(私有只读http:HttpService){}
@得到()
root():可观察{
返回此.httpClient.get('https://api.github.com/users/quen2404')
.pipe(map(response=>response.data));
}
}

您不能只返回整个
AxiosResponse
对象,因为它无法序列化为JSON。您很可能希望得到如下响应
数据

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

或者使用
承诺

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

您必须确保将响应作为JSON进行处理,您可以将其作为承诺返回并获取数据,使用HttpService或axios两者之一

import { Get, Controller, HttpService } from '@nestjs/common';
@Controller()
export class AppController {
  constructor(private readonly http: HttpService) {}
  @Get()
      root(): {
        return this.http.get('https://api.github.com/users/quen2404')
        .toPromise()
        .then(res => res.data)
        .catch(err => /*handle error*/)
      }
}
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<any>{
    return this.httpClient.get('https://api.github.com/users/quen2404')
      .pipe(map(response => response.data));
  }
}
@Get()
root() {
  return this.http.get('https://api.github.com/users/januwA').pipe(
    map(response => response.data)
  );
}
@Get()
async root() {
  const response = await this.http.get('https://api.github.com/users/januwA').toPromise();
  return response.data;
}
import { Get, Controller, HttpService } from '@nestjs/common';
@Controller()
export class AppController {
  constructor(private readonly http: HttpService) {}
  @Get()
      root(): {
        return this.http.get('https://api.github.com/users/quen2404')
        .toPromise()
        .then(res => res.data)
        .catch(err => /*handle error*/)
      }
}