json - Node.js 服务器 : JSON. 字符串化深层对象

标签 json node.js mongodb object circular-dependency

问题很简单,但对我来说还没有完美回答(也许只对我来说但并不完全清楚..)

问题:我想用 mongo 从“collection.findOne”返回 MongoDB。可以使用 JSON.stringify() 将此信息发送到另一个服务...

// i past a pseudo code for response :-)
collection.find({id_to_find: id_to_find}, function(err, results) {
 if (err){
    console.log ("error find");
 }if (results) { // update for good syntax !
    var results = JSON.stringify(results); // error, why ??? <if not this line, is ok, but i want stringify !>
    res.json({
        returnJSON: results
    });
 }
}
////////////////////////////////////
// example of a mongo object return :
 [ { _id: 1,
    property: 'xxxx',
    etc: 'xx'
   },
  { _id: 2
    property: 'xxxx',
    etc: 'xxxx'
  }
 ]

下次,我有几条记录,例如 results_mongo = [{mongo object datas},{ etc.. }]//就像一个数组

我想用我的服务器node.js来JSON.stringify我的集合&&返回这些结果..

The error is ::::: 

    TypeError: Converting circular structure to JSON
    at Object.stringify (native)

回应?

(感谢正确的对象结果,我的问题是如何排除字符串化深层对象..:)

对于搜索, cargo 在堆栈上发布: Convert Mongoose docs to json

How do you turn a Mongoose document into a plain object?

最佳答案

// you are passing object to your stringify method.
var object = JSON.stringify(object); 

// you need to pass results as your callack method returns results
var object = JSON.stringify(results); 

关于json - Node.js 服务器 : JSON. 字符串化深层对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28918662/

相关文章:

c# - 将原始 JSON 字符串存储在反序列化的 JSON.NET 对象中

javascript - 遍历数组并从循环中调用数据函数

java - 用户搜索功能使用 Java 驱动程序查询 MongoDB 中的数据

javascript - Nightmare.js找不到CSS标签

java - Elasticsearch Java api - 选择查询以搜索混合有特殊字符的文本(John-son)

node.js - 为 meteor 的 mongo 数据库设置密码

python - Flask 中的基本身份验证仅适用于一名用户

iphone - 在 iPhone 上使用 json-framework 需要帮助

javascript - Highcharts – 如何将轴显示为时间

linux - nodejs v5.10.1 无法再获取我的主机地址?