【发布时间】:2013-06-04 17:07:15
【问题描述】:
我使用 node.js、express.js 和 mongodb 创建了一个 RESTful API。我开始通过从完美运行的 MongoDB 集合中提取文档来创建路由。
示例收集文档
{
"_id" : ObjectId("51ace8c04cc8ea865df0923e"),
"title" : "Some Example Title",
"producer" :
{
"company" : "Your Company Name"
}
}
有效 - 如果我执行 .find({query}) 而不是通用 find()
,它也有效 app.get('/something', something.findAll);
exports.findAll = function(req, res) {
db.collection('something', function(err, collection) {
collection.find().toArray(function(err, items) {
res.contentType('json');
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET, PUT');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.send(items);
});
});
};
但是当我尝试使用 dot.notation 调用嵌入文档(即子文档)时,它会中断。
*不起作用*
db.something.find( { 'producer.company': 'ABC123' } )
或者即使我尝试
db.something.find( {producer: {company: 'ABC123'} } );
我收到一条错误消息。
TypeError: Converting circular structure to JSON
at Object.stringify (native)
at ServerResponse.res.json (../lib/response.js:185:19)
at ServerResponse.res.send (..//lib/response.js:117:21)
at ../routes/recipes.js:81:8
at Collection.find (../node_modules/mongodb/lib/mongodb/collection.js:931:5)
at ../routes/recipes.js:73:14
at Db.collection (../lib/mongodb/db.js:462:44)
at exports.findByCat (../routes/recipes.js:72:5)
at callbacks (../node_modules/express/lib/router/index.js:161:37)
at param (../node_modules/express/lib/router/index.js:135:11)
谁能帮我找到解决方法,或者让我知道我的方法是否有任何错误。
谢谢!
【问题讨论】:
-
您能描述一下收藏中的内容吗?否则,我无法向您提供更多您可以从该错误消息中获得的信息。
-
这表明 json 序列化程序正在运行循环参数
标签: node.js mongodb express mongo-collection