【问题标题】:NodeJS Express - TypeError: Converting circular structure to JSONNodeJS Express - TypeError:将循环结构转换为 JSON
【发布时间】:2021-03-03 14:14:19
【问题描述】:

好吧,我只是想构建一个简单的 expressJS 应用程序,但似乎没有任何效果,甚至这段代码也不行:

const express = require('express');
const bodyParser = require('body-parser');
require('dotenv').config();

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

/* require('./src/routes/index')(app); */

app.post('/', async (req, res) => {
    try {
        res.send(req);
    } catch(e) {
        console.log(e);
        res.send({ error: e.message });
    }
});

app.listen(process.env.PORT, () => {
    console.log(`Server initialized. Try it on http://localhost:${process.env.PORT}`);
})

每次我尝试发布这个简单的 JSON 请求时:

{
    "name": "namesample",
    "email": "emailsample",
    "password": "passwordsample"
}

我收到此错误:

{
  "error": "Converting circular structure to JSON\n    --> starting at object with constructor 'Socket'\n    |     property 'parser' -> object with constructor 'HTTPParser'\n    --- property 'socket' closes the circle"
}

console.log 打印:

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Socket'
    |     property 'parser' -> object with constructor 'HTTPParser'
    --- property 'socket' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify (C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\response.js:1123:12)
    at ServerResponse.json (C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\response.js:260:14)
    at ServerResponse.send (C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\response.js:158:21)
    at C:\Users\joaov\Documents\Github Projects\jwt-study\app.js:14:13
    at Layer.handle [as handle_request] (C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\router\index.js:281:22

有人可以帮忙吗?我的代码无法识别我的请求!!!

谢谢

【问题讨论】:

    标签: node.js json express


    【解决方案1】:

    如果您只是想检查您的发布请求是否有效,您可以更改 res.send(req);res.send(req.body);

    如果你想看到整个请求对象本身安装循环json节点模块,你可以进行如下更改。

    const express = require('express');
    const bodyParser = require('body-parser');
    const circularJSON = require('circular-json');
    require('dotenv').config();
    
    const app = express();
    
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    
    app.post('/', async (req, res) => {
        try {
            let json = circularJSON.stringify(req);
            res.send(json);
        } catch(e) {
            console.log(e);
            res.send({ error: e.message });
        }
    });
    
    app.listen(process.env.PORT, () => {
        console.log(`Server initialized. Try it on http://localhost:${process.env.PORT}`);
    })
    

    【讨论】:

    • 这实际上是一个带有额外信息的非常好的答案! Idk 为什么,req.body 也没有工作,但现在它才开始正常工作:D 关于 circularJSON 的东西很有用,顺便说一句,谢谢人:D
    猜你喜欢
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 2019-08-25
    • 2018-10-05
    • 2016-01-22
    • 2017-06-29
    • 2019-01-05
    • 1970-01-01
    相关资源
    最近更新 更多