TypeError:在Node.js中将循环结构转换为JSON [英] TypeError: Converting circular structure to JSON in nodejs

查看:84
本文介绍了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);

这篇关于TypeError:在Node.js中将循环结构转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆