TypeError:在Node.js中将循环结构转换为JSON
问题描述:
我正在使用针对Node.js的请求包
I am using request package for nodejs
我在这里使用此代码
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) {
和
exports.Register = function(req, res) {
res.header("Access-Control-Allow-Origin", "*");
console.log("Request data " +JSON.stringify(req));
我在这里遇到此错误TypeError:将圆形结构转换为JSON
任何人都可以告诉我是什么问题
Can anybody tell me what is the problem
谢谢
答
JSON不接受圆形对象-引用自己的对象.如果JSON.stringify()
遇到其中之一,则会引发错误.
JSON doesn't accept circular objects - objects which reference themselves. JSON.stringify()
will throw an error if it comes across one of these.
请求(req
)对象本质上是圆形的-Node做到了.
The request (req
) object is circular by nature - Node does that.
在这种情况下,因为您只需要将其登录到控制台,就可以使用控制台的本机字符串化并避免使用JSON:
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:
console.log("Request data:");
console.log(req);