TypeError:从具有构造函数“ClientRequest”属性“socket”的对象开始的 JSON 循环结构 -> 具有构造函数“Socket”的对象

2024-04-20

当我尝试使用 axios 向我自己的 typeorm API 发出发布请求时,出现以下错误:

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)

我尝试使用一些库来修复循环结构 JSON 并解析它,但都失败了:

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

实体的关系都没有添加级联选项。

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

我想要保留在数据库中的对象的数据示例:

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": "怪物事変"
            }
        ]
    }
}

如果我尝试通过 Postman 发布此 JSON 它可以正常工作。但不是通过代码,可能是由于实体的循环问题


正如@backtick所说,我应该做 res.send(response.data) 而不是整个响应对象。谢谢

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TypeError:从具有构造函数“ClientRequest”属性“socket”的对象开始的 JSON 循环结构 -> 具有构造函数“Socket”的对象 的相关文章