繁体   English   中英

如何使用Node.js在mysql上进行嵌套查询?

[英]How to make a nested queries on mysql with Node.js?

我正在尝试使用Express在Node.js上使用mysql进行嵌套查询,结果应该是带有三个嵌套的json,问题是返回的结果没有显示第三个嵌套,我的代码是:

app.get('/devices/:id', ensureToken, function(req, res){    
    jwt.verify(req.token, jwt_secret, function(err, data) {
        if (err) {
          res.sendStatus(403);
        } else {
            var segment_id = req.param('id');

        //Select devices for segment        
        var ls_devices = 'SELECT d.device_id AS id, d.device_name AS name, d.device_serial AS serial, d.device_description AS description, d.device_key AS keyatom, d.device_type AS type_id, dt.device_type_name AS type, d.device_user_id AS user_id, u.user_name AS username, d.device_segment_id AS segment_id, sg.segment_name, d.device_public AS isPublic, d.device_status AS status FROM ((g_devices d LEFT JOIN g_user u ON u.user_id = d.device_user_id) LEFT JOIN g_device_type dt ON dt.device_type_id = d.device_type) LEFT JOIN g_segments sg ON sg.segment_id = d.device_segment_id WHERE d.device_status = 1 AND sg.segment_id = '+segment_id;
        connection.query(ls_devices, function (error, results, fields) {
                  if (error) throw error;
                  if(results.length != 0) {                                                     
                      var j = JSON.parse(JSON.stringify(results));
                      var i = 0;
                      var d = [];
                    j.forEach(function(r,index_r){                        
                          //
                          var ls_controllers = 'SELECT c.device_controller_id AS id, c.device_controller_name AS name, c.device_controller_description AS description, c.device_controller_devcon_type AS type_id, ct.devcon_name AS type, ct.devcon_description AS description, d.device_name AS device, d.device_id AS device_id, d.device_serial AS serial, c.device_controller_public AS public, c.device_controller_date_register AS registered, c.device_controller_date AS date, c.device_controller_status AS status  FROM (g_device_controller c LEFT JOIN g_device_controller_type ct ON ct.devcon_id = c.device_controller_devcon_type) LEFT JOIN g_devices d ON d.device_id = c.device_controller_device_id WHERE c.device_controller_status = 1 AND d.device_id = '+r.id;
                          connection.query(ls_controllers, function (error_c, results_c, fields_c) {                            
                            var k = JSON.parse(JSON.stringify(results_c));
                            d.push({device:r.name,controller:[]})
                            k.forEach(function(r2,index_r2){                                                                        
                                d[index_r].controller.push({name:r2.name,action:[]})
                                var ls_actions = 'SELECT a.action_id AS id, a.action_name AS name, a.action_description AS description, a.action_type AS type_id, aty.action_type_name, aty.action_type_description AS type_description, a.action_controller_device_id AS device_id, a.action_control AS control, a.action_value AS value, a.action_time AS time, a.action_times AS times, a.action_date AS date, a.action_status AS status  FROM g_actions a LEFT JOIN g_action_type aty ON aty.action_type_id = a.action_type WHERE a.action_controller_id = '+r2.id;
                                  connection.query(ls_actions, function (error_a, results_a, fields_a) {                                    
                                    if(results_a.length > 0) {
                                        var l = JSON.parse(JSON.stringify(results_a));
                                        l.forEach(function(r3, index_r3){                                                                                                               
                                            d[index_r].controller[index_r2].action.push({id_a:r3.id,name_a:r3.name});                                           
                                            console.log(JSON.stringify(d))
                                        });                                     
                                    }                                   
                                });
                            });                                           
                            i ++;                           
                            if(i == j.length)
                            {                               
                                return res.json(d); 
                            }                           
                          });                         
                    })
                }
              else {
                return res.json({devices:false});   
              }         
            }); 
    }
    });     
});

我的网络回复是:

[
    {
        "device": "device one",
        "controller": [
            {
                "name": "device controller One",
                "action": []
            }
        ]
    },
    {
        "device": "device two",
        "controller": []
    }
]

最后一次数组推送的打印结果是:

[{“设备”:“设备一”,“控制器”:[{“名称”:“设备控制器一”,“动作”:[{“ id_a”:1,“ name_a”:“设备动作一”}] }]},{“设备”:“设备二”,“控制器”:[

]

}]

k.forEach()不等待connection.query()完成,因此有效地跳过了查询。 补救措施是执行与j.forEach()类似的操作,但是在所有这些都完成之前,i ++也不应该发生。

(其他说明:您可以使用promises ,或者async/await ,使流程看起来更加整洁,或者,如果您想继续学习回调,可以使用async库来简化其中的一部分)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM