【发布时间】:2015-05-31 22:33:20
【问题描述】:
在对数据库进行查询时,我在操作 json 时遇到了问题。我有一个具有以下属性的表类型:id、name、parent_id。我想获取每个 id=null 的流派,并为每个流派添加属性 children 以及 parent_id 等于其 id 的流派。
这是我到目前为止所做的:
getGenres: function (req, res) {
Genres.find().where({parent_id : null}).exec(function(err, elements) {
if (err)
console.log(err);
else {
async.each(elements, function(item, callback) {
Genres.find().where({parent_id: item.id}).exec(function(err, items) {
console.log(items);
item['children'] = items; //Doesn't work but that's the idea
callback();
})
})
res.send(elements);
}
})
} 我得到了我想要的项目,我只是不知道如何将它们添加到 json 中。
【问题讨论】:
标签: json node.js asynchronous