问题很简单,但对我来说还没有完美回答(也许只对我来说但并不完全清楚..)
问题:我想用 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
最佳答案
// 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/