Question: Converting circular structure to JSON
Kolesar opened this issue · comments
Asim Husanović commented
Hi there,
I am very new to NestJS and something I did wrong. If you can help me I will be very thankful :)
Module:
import { Global, Module } from '@nestjs/common';
import KeycloakService from './keycloak.service';
import { HttpModule } from 'nestjs-http-promise';
@Global()
@Module({
imports: [
HttpModule.register({
timeout: 10000,
retries: 3,
retryDelay: () => {
return 500;
},
baseURL: 'https://my-local-domain.local',
headers: {
Accept: 'application/json',
'Content-type': 'application/json',
},
}),
],
providers: [LocalService],
exports: [LocalService],
})
class LocalModule {}
export default LocalModule;
Service:
import { Injectable } from '@nestjs/common';
import { HttpService } from 'nestjs-http-promise';
@Injectable()
class LocalService {
constructor(private httpService: HttpService) {}
public async callSomeServer(): Promise<any> {
return await this.httpService.get('/users');
}
}
export default LocalService;
Controller:
import { Controller, Get } from '@nestjs/common';
import LocalService from './local.service';
@Controller()
export class LocalController {
constructor(private readonly localService: LocalService) {}
@Get('/user')
public async userCall(): Promise<any> {
const ret = await this.localService.callSomeServer();
console.log(ret);
return ret;
}
}
In the console, I get a response from the console.log(ret)
but after that I have an error:
ERROR [ExceptionsHandler] Converting circular structure to JSON
--> starting at object with constructor 'ClientRequest'
| property 'socket' -> object with constructor 'TLSSocket'
--- property '_httpMessage' closes the circle
Asim Husanović commented
I have found a solution (It was fast :) )
In controller return have to be:
return ret.data;
benhason1 commented
Hi, the response of axios request contains complex data structure with circular dependency in it which cause the problem.
Instead of logging the whole response I think you may want to log only the response: console.log(ret.data)
.
Also you can check this library which can flat circular json response.