Nestjs using axios

后端 未结 3 1237
[愿得一人]
[愿得一人] 2021-02-05 19:56

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

import { Get, Controller, HttpService } fro         


        
3条回答
  •  既然无缘
    2021-02-05 20:33

    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)
      );
    }
    

    or alternatively using Promises:

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

提交回复
热议问题