最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: circular structure to JSON starting at object with constructor 'ClientRequest' property

programmeradmin9浏览0评论

Im getting the following error when I try to make post request to my own typeorm API using axios:

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 (C:\Users\Usuario\Documents\Manga-Api\Manga-Api\node_modules\express\lib\response.js:1123:12)
    at ServerResponse.json (C:\Users\Usuario\Documents\Manga-Api\Manga-Api\node_modules\express\lib\response.js:260:14)
    at ServerResponse.send (C:\Users\Usuario\Documents\Manga-Api\Manga-Api\node_modules\express\lib\response.js:158:21)
    at C:\Users\Usuario\Documents\Manga-Api\Manga-Api\src\managers\scrape.manager.ts:163:33
    at processTicksAndRejections (node:internal/process/task_queues:93:5)

I have tried to use some libraries to fix the circular structure JSON and parse it but both failed:

const safeStringify = require('json-stringify-safe');
const CircularJSON = require('circular-json');

None of the entities's relations have cascade option added.

await axios.post(apiName+'/object', data, { headers: { Authorization: res.req.headers.authorization } }).then(response => { res.send(response); });

Data example with the object I want to persist in my database:

data = {
    "response": "Manga created",
    "manga": {
        "magazine": {
            "name": "JUMP SQ.",
            "japanName": "ジャンプSQ.",
            "website": "/",
            "releaseDate": "",
            "id": 33,
            "mangas": [
                {
                    "finished": false,
                    "id": 91,
                    "chapter": 312,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 166,
                    "chapter": 201,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 175,
                    "chapter": 85,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 202,
                    "chapter": 95,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 363,
                    "chapter": 94,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 366,
                    "chapter": 124,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 456,
                    "chapter": 46,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 515,
                    "chapter": 50,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 520,
                    "chapter": 14,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 567,
                    "chapter": 14,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 1024,
                    "chapter": 0,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                }
            ]
        },
        "author": {
            "name": "AIMOTO SHOU",
            "japanName": "",
            "id": 417,
            "mangas": [
                {
                    "finished": false,
                    "id": 456,
                    "chapter": 46,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 1024,
                    "chapter": 0,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                }
            ]
        },
        "languages": [
            {
                "code": 0,
                "name": "Kemono Jihen"
            },
            {
                "code": 1,
                "name": "怪物事変"
            }
        ]
    }
}

If I try to post this JSON via Postman It works propperly. Buy not by code, maybe due the circular problem with the Entities

Im getting the following error when I try to make post request to my own typeorm API using axios:

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 (C:\Users\Usuario\Documents\Manga-Api\Manga-Api\node_modules\express\lib\response.js:1123:12)
    at ServerResponse.json (C:\Users\Usuario\Documents\Manga-Api\Manga-Api\node_modules\express\lib\response.js:260:14)
    at ServerResponse.send (C:\Users\Usuario\Documents\Manga-Api\Manga-Api\node_modules\express\lib\response.js:158:21)
    at C:\Users\Usuario\Documents\Manga-Api\Manga-Api\src\managers\scrape.manager.ts:163:33
    at processTicksAndRejections (node:internal/process/task_queues:93:5)

I have tried to use some libraries to fix the circular structure JSON and parse it but both failed:

const safeStringify = require('json-stringify-safe');
const CircularJSON = require('circular-json');

None of the entities's relations have cascade option added.

await axios.post(apiName+'/object', data, { headers: { Authorization: res.req.headers.authorization } }).then(response => { res.send(response); });

Data example with the object I want to persist in my database:

data = {
    "response": "Manga created",
    "manga": {
        "magazine": {
            "name": "JUMP SQ.",
            "japanName": "ジャンプSQ.",
            "website": "https://jumpsq.shueisha.co.jp/sq/",
            "releaseDate": "",
            "id": 33,
            "mangas": [
                {
                    "finished": false,
                    "id": 91,
                    "chapter": 312,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 166,
                    "chapter": 201,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 175,
                    "chapter": 85,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 202,
                    "chapter": 95,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 363,
                    "chapter": 94,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 366,
                    "chapter": 124,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 456,
                    "chapter": 46,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 515,
                    "chapter": 50,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 520,
                    "chapter": 14,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 567,
                    "chapter": 14,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 1024,
                    "chapter": 0,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                }
            ]
        },
        "author": {
            "name": "AIMOTO SHOU",
            "japanName": "",
            "id": 417,
            "mangas": [
                {
                    "finished": false,
                    "id": 456,
                    "chapter": 46,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                },
                {
                    "finished": false,
                    "id": 1024,
                    "chapter": 0,
                    "state": false,
                    "published": false,
                    "updated": false,
                    "priority": 0
                }
            ]
        },
        "languages": [
            {
                "code": 0,
                "name": "Kemono Jihen"
            },
            {
                "code": 1,
                "name": "怪物事変"
            }
        ]
    }
}

If I try to post this JSON via Postman It works propperly. Buy not by code, maybe due the circular problem with the Entities

Share edited Dec 26, 2020 at 17:59 Miguelo asked Dec 26, 2020 at 17:48 MigueloMiguelo 1411 gold badge2 silver badges9 bronze badges 2
  • 2 I think res.send might be expecting something other than an entire response object. – backtick Commented Dec 26, 2020 at 18:05
  • 2 @backtick You were right, I was supposed to do res.send(response.data) not the whole response object. Such a noob fail hahaha thank you bro!! – Miguelo Commented Dec 26, 2020 at 18:11
Add a ment  | 

2 Answers 2

Reset to default 5

As @backtick said, I was supposed to do res.send(response.data) not the whole response object. Thank you

TypeError: JSON circular structure starting from the object

Delete that property from the object you want to return that caused this error or loop

Example:

Delete object.property in your case Delete object.socket

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论