TypeError:将圆形结构转换为JSON->从具有构造函数'ClientRequest'的对象开始

时间:2020-11-08 07:47:14

标签: typescript axios nestjs

我是nest.js的初学者,我正在尝试用我的代码实现Axios,并且发生此错误,我想对其进行修复。

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

这是我的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;

    }

这是我的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);
    }

谢谢您的帮助:)

2 个答案:

答案 0 :(得分:2)

首先,nest.js为您提供了HttpService,可以通过DI注入它: https://docs.nestjs.com/techniques/http-module

第二个-您正在尝试存储整个响应对象,该对象是复杂的数据结构,并且包含错​​误消息中所述的循环依赖关系(TypeError:将循环结构转换为JSON)

您应该做的是映射所需的数据,而不是存储整个圆形对象,

或者您应该查看一些可以解析循环json的库:https://www.npmjs.com/package/flatted

答案 1 :(得分:1)

而不是存储作为循环对象的整个响应对象。您可以存储响应的 data 对象键。那应该工作得很好