TypeError: Converting circular structure to JSON --> starting at object with constructor 'ClientRequest'

Viewed 73557

I am a nest.js beginner and I am trying to implement Axios with my code and this error occurs and I would like to fix it.

    --> starting at object with constructor 'ClientRequest'
    |     property 'socket' -> object with constructor 'Socket'
    --- property '_httpMessage' closes the circle +188941ms
TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'ClientRequest'
    |     property 'socket' -> object with constructor 'Socket'
    --- property '_httpMessage' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify (D:\CUSportcomplex-register\sso-reg\node_modules\express\lib\response.js:1123:12)
    at ServerResponse.json (D:\CUSportcomplex-register\sso-reg\node_modules\express\lib\response.js:260:14)
    at ExpressAdapter.reply (D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\platform-express\adapters\express-adapter.js:24:57)
    at RouterResponseController.apply (D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-response-controller.js:13:36)
    at D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-execution-context.js:173:48
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-execution-context.js:47:13
    at async D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-proxy.js:9:17

This is my app.service.ts

async validateSSO(appticket): Promise<SsoContent> {
        let instance = axios.create({
            baseURL: "http://localhost:8080/",
            headers: {
                'DeeAppId': config.DeeAppId,
                'DeeAppSecret': config.DeeAppSecret,
                'DeeTicket': appticket
            }
        });
        instance.get("/serviceValidation")
            .then(response => {
                this.ssoContent = response;
            })
            .catch(error => {
                return (error);
            });

        return this.ssoContent;

    }

and this is my app.controller.ts

    @Get('validation/:appticket')
    async validateSSO(
        @Param('appticket') appticket: string
        //DeeAppTicket is sented via Front-end
    ): Promise<SsoContent> {
        return this.registeringService.validateSSO(appticket);
    }

Thank you for your help :)

7 Answers

First of all nest.js provides you HttpService out of the box that you may inject it through DI: https://docs.nestjs.com/techniques/http-module

Second - you are trying to store whole response object which is complex data structure and contains circular dependencies as it stated in error message (TypeError: Converting circular structure to JSON)

What you should do is either you map the data you need instead of storing whole circular object,

or you should look up to some libs that could parse circular json: https://www.npmjs.com/package/flatted

Rather than storing the whole response object which is a circular object. You can store the data object key of the response. That should work just fine

This also happens when you forget to put the keyword await in your async function.

Today I had this problem and one way I managed to solve it was instead of return:

this.ssoContent = response;

I returned:

this.ssoContent = response.data;

'This also happens when you forget to put the keyword await in your async function.'

solved for me.

how silly I feel....

I have that's problem when my app(nest.js app) try to send request to another apps(Nest.js apps). The problem is response of server. I can not resolve it, instead use superagent library. https://www.npmjs.com/package/superagent It is resolved easily.

I am using TypeORM with NestJS and got several times the following error:

TypeError: Converting circular structure to JSON
--> starting at object with constructor 'EntityMetadata'
|     property 'ownColumns' -> object with constructor 'Array'
|     index 0 -> object with constructor 'ColumnMetadata'
--- property 'entityMetadata' closes the circle

It was because I forgot the getOne() or getMany() at the end of my query builder.

Related