【问题标题】:Node JS - express - postgres obtain nested json objects by loop queriesNode JS-express-postgres通过循环查询获取嵌套的json对象
【发布时间】:2016-02-07 17:41:29
【问题描述】:

我正在使用 postgres 在节点 js 上尝试以下代码来获取嵌套 json 对象的列表,如下所示:

{
    "elements": [
        {
            "name": "element 1",
            "description": "lorem ipsus",
            "docs": [
                {
                    "docs_id": 1053,
                    "docs_file": "../uploads/1461s444---planimetria.pdf",
                    "docs_created_at": "ThuMay14201506: 00: 00GMT+0200(CEST)"
                },
                {
                    "docs_id": 1054,
                    "docs_file": "../uploads/1461s444---VAX-with-highlight.pdf",
                    "docs_created_at": "ThuMay14201506: 00: 00GMT+0200(CEST)"
                },
                {
                    "docs_id": 1055,
                    "docs_file": "../uploads/1461s444---Elaborato-Planimetrico-with-highlight.pdf",
                    "docs_created_at": "ThuMay14201506: 00: 00GMT+0200(CEST)"
                }
            ]
        },
        {
            "name": "element 2",
            "description": "lorem ipsus",
            "docs": [
                {
                    "docs_id": 1056,
                    "docs_file": "../uploads/pianta.pdf",
                    "docs_created_at": "ThuMay17201106: 00: 00GMT+0200(CEST)"
                },
                {
                    "docs_id": 1055,
                    "docs_file": "../uploads/test.pdf",
                    "docs_created_at": "ThuMay16201706: 00: 00GMT+0200(CEST)"
                }
            ]
        }
    ]
}

使用此代码:

apiRoutes.get('/elements', function(req, res) {

var elements = [];  
knex.select().table('element').where(req.query)
    .then (function(rows){
        for(var i in rows){
            var element =  rows[i];
            knex.select().table('document').where('element_id',rows[i].element_id)
                .then (function(docs){
                    element['docs'] = docs;
                    //console.log (elements);
                    elements.push(element);
                });
        }
        res.json (elements);
    });
});

但结果是一个空数组。

我认为这是关于查询的异步处理的问题,但我无法解决。

【问题讨论】:

    标签: json node.js postgresql express knex.js


    【解决方案1】:

    您的 for 循环正在进行大量异步调用。您需要在承诺中归还所有这些。这是使用bluebird 的示例。 (我还没有对此进行测试。)

    var Promise = require('bluebird')
    
    knex.select().table('element').where(req.query)
        .then (function(rows){
    
            var promises = rows.map(function(element){
                return knex.select().table('document').where('element_id',element.element_id)
                    .then(function(docs){
                        element['docs'] = docs
    
                        return element;
                    });
            })
    
           return Promise.all(promises)     
        }).then(function(elements){     
            res.json (elements);
        })
    });
    

    但是,我认为发出第二个查询是n+1。 Knex 支持left outer join。你应该使用它。

    【讨论】:

    • 工作,谢谢乔希!另一个问题......如果我要在主查询“元素”中处理其他子查询(如“文档”),那么最好的方法是什么
    • 我认为您的意思是使用左外连接。对于 Stackoveflow 上的单独问题,这可能会更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2013-03-18
    相关资源
    最近更新 更多