Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :)

like image 749
ChuChuwi Avatar asked Nov 08 '20 07:11

ChuChuwi


People also ask

How do I fix TypeError converting circular structure to JSON?

The "Converting circular structure to JSON" error occurs when we pass an object that contains circular references to the JSON. stringify() method. To solve the error, make sure to remove any circular references before converting the object to JSON.

Can t log message Converting circular structure to JSON?

TypeError: Converting circular structure to JSON occurs when you try to reference your variable name within the JSON object. The code above will give an error since JSON. stringify is unable to convert such structures. This also happens to DOM nodes with circular references.

What is circular structure in JSON?

A circular structure is an object that references itself. To be able to stringify such objects, developers can utilize the replacer parameter in the stringify() method, making sure the function that is being passed in, filters out repeated or circular data.

What is TypeError converting circular structure to JSON?

What is TypeError: Converting circular structure to JSON? TypeError: Converting circular structure to JSON occurs when you try to reference your variable name within the JSON object. The code above will give an error since JSON.stringify is unable to convert such structures.

What is a circular reference in JSON?

We set a property on the object that points to the object itself - that's a circular reference. The JSON.stringify method does not support circular references, so we have to remove them before converting the object to JSON.

How to solve the error when converting an object to JSON?

To solve the error, make sure to remove any circular references before converting the object to JSON. Here are some examples of how the error occurs. We set a property on the object that points to the object itself - that's a circular reference.

How to solve the circular reference error in JavaScript engine?

That's why JavaScript engine gives you an error when you convert a circular reference object to JSON string. How to solve it? You could transfer the object to another object and except or delete circular attributes.


Video Answer


2 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

like image 88
LuckyLuke Avatar answered Sep 20 '22 21:09

LuckyLuke


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

like image 28
mykoman Avatar answered Sep 20 '22 21:09

mykoman