Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
152
rated 0 times [  155] [ 3]  / answers: 1 / hits: 60477  / 6 Years ago, wed, october 17, 2018, 12:00:00

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


What should I do?



[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

More From » node.js

 Answers
77

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

[#53304] Friday, October 12, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alorac

Total Points: 262
Total Questions: 82
Total Answers: 97

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
alorac questions
Sat, Oct 10, 20, 00:00, 4 Years ago
Tue, Sep 22, 20, 00:00, 4 Years ago
Wed, Jul 1, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Sun, May 17, 20, 00:00, 4 Years ago
;