Javascript TypeError:在nodejs中将圆形结构转换为JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27101240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: Converting circular structure to JSON in nodejs
提问by Hitu Bansal
I am using request package for nodejs
我正在为 nodejs 使用请求包
i am using this code here
我在这里使用这个代码
var formData = ({first_name:firstname,last_name:lastname,user_name:username, email:email,password:password});
request.post({url:'http://localhost:8081/register', JSON: formData}, function(err, connection, body) {
and
和
exports.Register = function(req, res) {
res.header("Access-Control-Allow-Origin", "*");
console.log("Request data " +JSON.stringify(req));
here i am getting this error TypeError: Converting circular structure to JSON
在这里我收到此错误类型错误:将循环结构转换为 JSON
Can anybody tell me what is the problem
谁能告诉我是什么问题
Thanks
谢谢
回答by Scimonster
JSON doesn't accept circular objects - objects which reference themselves. JSON.stringify()will throw an error if it comes across one of these.
JSON 不接受循环对象 - 引用自身的对象。JSON.stringify()如果遇到其中之一,将抛出错误。
The request (req) object is circular by nature - Node does that.
request ( req) 对象本质上是循环的——Node 就是这样做的。
In this case, because you just need to log it to the console, you can use the console's native stringifying and avoid using JSON:
在这种情况下,因为您只需要将其记录到控制台,您可以使用控制台的原生字符串化并避免使用 JSON:
console.log("Request data:");
console.log(req);
回答by Dinesh Nadimpalli
Try using this npm package. This helped me decoding the res structure from my node while using passport-azure-adfor integrating login using Microsoft account
尝试使用这个 npm 包。这帮助我从我的节点解码 res 结构,同时passport-azure-ad用于使用 Microsoft 帐户集成登录
https://www.npmjs.com/package/circular-json
https://www.npmjs.com/package/circular-json
You can stringify your circular structure by doing:
您可以通过执行以下操作来字符串化您的循环结构:
const str = CircularJSON.stringify(obj);
then you can convert it onto JSON using JSON parser
然后您可以使用 JSON 解析器将其转换为 JSON
JSON.parse(str)
回答by Ravi Jain
TypeError: Converting circular structure to JSON in nodejs:This error can be seen on Arangodb when using it with Node.js, because storage is missing in your database. If the archive is created under your database, check in the Aurangobi web interface.
TypeError: Converting circular structure to JSON in nodejs:将 Arangodb 与 Node.js 一起使用时,可以在 Arangodb 上看到此错误,因为您的数据库中缺少存储。如果存档是在您的数据库下创建的,请检查 Aurangobi Web 界面。
回答by satya prakash
I also ran into this issue. It was because I forgot to await for a promise.
我也遇到了这个问题。那是因为我忘记了等待一个承诺。
回答by Mohit Gupta
use this https://www.npmjs.com/package/json-stringify-safe
使用这个https://www.npmjs.com/package/json-stringify-safe
var stringify = require('json-stringify-safe');
var circularObj = {};
circularObj.circularRef = circularObj;
circularObj.list = [ circularObj, circularObj ];
console.log(stringify(circularObj, null, 2));
stringify(obj, serializer, indent, decycler)

