从两个查询和一个连接表构建json对象的最佳方法

时间:2018-02-09 10:26:43

标签: javascript json node.js web-services express

我目前正致力于节点中的小型网络服务,用于管理餐厅的命令。我正在使用Express.js

在我的数据库中我得到了这些表:

DISH(id,name)
COMMAND(id,table,status)
COMMAND_DISH(idCommand,idDish,quantity)

我想知道是否有办法为每个命令构建这样的JSON对象:

[
  {
    "idCommand": 1,
    "table": 21,
    "dishes": {
      "dish1": {
        "dishName1": "salad",
        "quantity": 2
      },
      "dish2": {
        "dishName1": "steak",
        "quantity": 2
      }
    }
  }
]

知道我的查询会返回这样的行

命令:

id table status
1    3    sent
2    4    sent
3    5    sent

Command_dish:

idCommand idDish quantity
    1       2       2
    1       3       2
    2       2       4
    2       1       1
    2       3       5

菜:

id name
1  salad
2  steak
3  pasta
4  pizza

我无法弄清楚当我从command_dish获得多个commandId时如何构造json对象

编辑:到目前为止,这是我尝试构建数据的方式,但我对查询中的查询不确定,是否可行?

app.get("/commandes", function(req, res, next) {
var restaurant = req.query.restaurant;
connection.connect();
var tableauCommande= new Object();
connection.query('SELECT * FROM COMMANDE WHERE restaurant=?',restaurant,function(err, rows, fields) {
    if (err) throw err;
    for (var i = 0; i < rows.length; i++) {
        var tableauResult = new Object();
        tableauResult["idCommande"]=rows[i].id;
        tableauResult["table"]=rows[i].taable;      
        var tableauPlats = new Object();
        connection.query('SELECT P.label, C.quantite FROM PLAT AS P, COMMANDE_PLAT AS C WHERE C.plat=P.id AND C.commande=?',rows[i].id,function(err, rows, fields) {
            if (err) throw err;             
            for (var j = 0; j < rows.length; j++) {
                tableauPlats[rows[j].label]=rows[j].quantite;
            }
            tableauResult["plats"]=JSON.stringify(tableauPlats);
            JSON.stringify(tableauResult);
        });
        tableauCommande[rows[i].id]=JSON.stringify(tableauResult);
    }
});
});

EDIT2:我将代码更改为:

app.get("/commandes", function(req, res, next) {
var restaurant = req.query.restaurant;
var tableauResult= new Object();
connection.connect();   
connection.query('SELECT * FROM COMMANDE WHERE restaurant=?',restaurant,function(err, rows, fields) {
    if (err) throw err;
    for (var i = 0; i < rows.length; i++) {
        var tableauCommande = new Object();             
        tableauCommande["table"]=rows[i].taable;        
        tableauResult[rows[i].id]=tableauCommande;
    }
}); 
connection.end();
console.log(tableauResult);
for (var key in tableauResult) {
    var tableauPlats = new Object();
    connection.connect();               
    connection.query('SELECT P.label, C.quantite FROM PLAT AS P, COMMANDE_PLAT AS C WHERE C.plat=P.id AND C.commande=?',key,function(err, rows, fields) {
        if (err) throw err;             
        for (var j = 0; j < rows.length; j++) {
            tableauPlats[rows[j].label]=rows[j].quantite;
        }
        tableauResult[key]["plats"]=tableauPlats;
    });
    connection.end();
}
    console.log(tableauResult);
});

它似乎更稳定,但我得到了一个&#34;无法在调用quit后排队查询&#34;错误。因为我正确管理连接,所以我不明白

1 个答案:

答案 0 :(得分:0)

如果有人有兴趣,我设法像这样构建json对象

app.get("/commandes", function(req, res, next) {
var restaurant = req.query.restaurant;
var tableauResult= new Object();
connection.connect();   
connection.query('SELECT CP.commande, C.taable, C.status, P.label, CP.quantite FROM PLAT AS P, COMMANDE AS C, COMMANDE_PLAT AS CP WHERE CP.plat=P.id AND C.id=CP.commande AND C.restaurant=?',restaurant,function(err, rows, fields) {
    if (err) throw err;
    for (var i = 0; i < rows.length; i++) {
        if(rows[i].commande in tableauResult){
            tableauResult[rows[i].commande]["plats"][rows[i].label]=rows[i].quantite;
        }else{
            var tableauCommande = new Object();
            tableauCommande["table"]=rows[i].taable;
            tableauCommande["status"]=rows[i].status;               
            var tableauPlats = new Object();
            tableauPlats[rows[i].label]=rows[i].quantite;               
            tableauCommande["plats"]=tableauPlats;
            tableauResult[rows[i].commande]=tableauCommande;
        }
    }
    res.json(tableauResult);
});
connection.end();
});

棘手的是,即使你正确打开和关闭连接,似乎mysql节点包也不喜欢两个查询所以我在一个查询中加载了所有内容,我不得不自己对结果进行排序。