Nest 29840 - ExceptionsHandler Converting circular structure to JSON

Viewed 1047

NextJS backend issue while dealing with http post, It throws converting circular structure to JSON error.

Here is the code stack trace which details issue

[Nest] 29840   - 01/04/2021, 2:46:31 PM   ExceptionsHandler Converting circular structure to JSON
    --> starting at object with constructor 'ClientRequest'
    |     property 'socket' -> object with constructor 'TLSSocket'
    --- property '_httpMessage' closes the circle +199712ms
TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'ClientRequest'
    |     property 'socket' -> object with constructor 'TLSSocket'
    --- property '_httpMessage' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify node_modules\express\lib\response.js:1123:12)
    at ServerResponse.json \node_modules\express\lib\response.js:260:14)
    at ExpressAdapter.reply node_modules\@nestjs\platform-express\adapters\express-adapter.js:24:57)    
    at RouterResponseController.apply node_modules\@nestjs\core\router\router-response-controller.js:13:36)
    at \node_modules\@nestjs\core\router\router-execution-context.js:173:48
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
    at async node_modules\@nestjs\core\router\router-execution-context.js:47:13
    at async \node_modules\@nestjs\core\router\router-proxy.js:9:17
1 Answers

It took some time to figure out solution however this issue can be resolved by adding below code

this.http.post(https://api-enpont,body, headerData).pipe( map(response => response.data));

Do not forget to import { map } from 'rxjs/operators';

Related