如何在nodejs中使用promise执行两个mysql查询?

柯文

这是我要实现的目标,我想根据第一个 mysql 查询(数组 JSON 数据)的值加入从我的 node.js 服务器返回的 JSON 数据

如果我只想执行两个 mysql 查询,我只需启用multipleStatements: true那么代码将是这样的:

app.post('/product', function (req, res) {

connection.query('call getProductList; call rowCountTotal', function (err, rows, fields) {
    if (!err) {
        var response = [];

        if (rows.length != 0) {
            response.push({ 'result': 'success', 'data': rows });
        } else {
            response.push({ 'result': 'error', 'msg': 'No Results Found' });
        }

        res.setHeader('Content-Type', 'application/json');
        res.status(200).send(JSON.stringify(response));
    } else {
        res.status(400).send(err);
    }
});

});

比数据将显示在两个数组中分隔的两个 JSON,但我想在这里构建的是一个带有多个数组 JSON 数据的 JSON,如下所示:

我想要的示例 JSON:

[  
   {  
      "product_id":"1",
      "product_name":"MX-001",
      "product_attachment":[  
         {  
            "product_id":"1",
            "product_attachment_id":"1",
            "file_name":"maxgrand5.jpg",
            "file_path":"assets"
         }
      ]
   }
]

这是我在 node.js 服务器端代码中尝试做的,我尝试使用

Promise.all (i think this code i should use right?) :

    return new Promise(function(resolve, reject) {

        Promise.all(connection.query('call getProductSingle("'+ product_series +'")', function (err, rows, fields) {
            if (!err) {
                var response = [];

                if (rows.length != 0) {
                    response.push({ 'result': 'success', 'data': rows });
                } else {
                    response.push({ 'result': 'error', 'msg': 'No Results Found' });
                }

                connection.query('call getProductAttachment("'+ rows[0][0].product_id +'")', function (err, rowsAttachment, fields) {
                    if (!err) {
                        console.log("second query");
                        if (rowsAttachment.length != 0) {
                            response.push({'product_attachment': rowsAttachment });
                        } else {
                            response.push({ 'result': 'error', 'msg': 'No Results Found' });
                        }
                    }
                });
                console.log("outside second query");
                res.setHeader('Content-Type', 'application/json');
                res.status(200).send(JSON.stringify(response));
            } else {
                res.status(400).send(err);
            }

            console.log("last");
            if (err) {
                return reject(err);
            }
            resolve(res);
        }));
    });

这是我在 'getProductSingle' 中命名的存储过程结果:

  1. 产品 ID = 1
  2. 产品名称 = MX-001

这是我的第二个程序结果“getProductAttachment”:

  1. 产品 ID = 1
  2. 文件名 = maxgrand5.jpg
  3. file_path = 资产
  4. product_attachment_id = 1

一个 product_id 可以有 1 个以上 product_attachment_id

我怎样才能加入数据?

我刚刚更新了我的问题,问题是当我提出请求时第二个查询太晚了,我应该使用 promise 使其不迟到,怎么做?

妙罗

首先,我创建了查询,其中 product_single 表连接到 product_attachments,也许您想使用WHERE子句或使用LIMITand的分页机制来限制它OFFSET

SELECT ps.product_id, ps.product_name, pa.product_attachment_id,
pa.file_name, pa.file_path
FROM product_single ps
LEFT JOIN product_attachment pa
ON ps.product_id = pa.product_id
ORDER by ps.product_id,pa.product_attachment_id;

在下面的代码中,我将使用call product_join_att.

return new Promise(function(resolve, reject) {
    var result_products = [];
    var result_attachments = [];
    var old_id = undefined;
    var old_name = undefined;
    var new_id = undefined;
    var row_index = 0;
    connection.query('call product_join_att', function (err, rows, fields) {
        if (err) {
            reject(err);
            return;
        } else if (rows.length == 0) {
            reject(new Error('No Results found'));
            return;
        }
        while (row_index < rows.length  ) {
            new_id = rows[row_index].product_id;
            if (old_id !== new_id) { // any new line with an new id...
                if (typeof old_id !== 'undefined') { // when the old line is existing
                    result_products.push( // push the product
                        {"product_id": old_id.toString(),
                         "product_name":old_name,
                         "product_attachment": result_attachments
                        });
                }
                old_id = new_id; // remember the new_id
                old_name = rows[row_index].product_name;// and name
                product_attachments = []; // and initialize attachments.
            }
            product_attachments.push({ // provide the inner attachment informations.
                "product_id": new_id,
                "product_attachment_id" : rows[row_index].product_attachment_id,
                "file_name" : rows[row_index].file_name;
                "file_path" : rows[row_index].file_path;
            });
            row_index++; // and go to the next row.
        }
        if (typeof old_id !== 'undefined') { // if there are still data
            result_products.push( // push the last line also.
                {"product_id": old_id.toString(),
                 "product_name":old_name,
                 "product_attachment": result_attachments
                });
        }
    } // query
    resolve(result_products);
} // end of promise...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Prometheus中使用两个指标执行查询?

来自分类Dev

如何在MySQL中使用子查询显示两个表的混合结果?

来自分类Dev

如何在MySQL中使用子查询显示两个表的混合结果?

来自分类Dev

如何在VB中同时执行两个查询?(MySQL的)

来自分类Dev

如何在一个查询中使用两个COUNT

来自分类Dev

如何在PHP中使用一个mysqli查询更新两个选择框

来自分类Dev

如何让两个promise同步执行?

来自分类Dev

如何在C中使用open mp同时执行两个不同的功能

来自分类Dev

如何在SQL中使用两个相似的参数执行搜索功能

来自分类Dev

如何在bash中使用`fc`编辑和执行最后两个命令

来自分类Dev

在mysql查询中使用两个内部联接

来自分类Dev

在MySQL中使用子查询联接两个表

来自分类Dev

如何在具有by和count()的SQL中使用两个查询的并集

来自分类Dev

如何在SQL查询中使用两个联接?句法

来自分类Dev

如何在PHP中使用不同的查询添加两个不同的行

来自分类Dev

如何在React Native中使用两个Firestore查询获取数据

来自分类Dev

如何在XSLT XPath查询中使用“两个自我”?

来自分类Dev

如何在PHP中使用不同的查询添加两个不同的行

来自分类Dev

如何在Codeigniter中使用单个查询在两个单独的表中插入数据?

来自分类Dev

如何在 SQL 查询中使用两个 where 子句

来自分类Dev

如何在 UNION 查询中的两个表中使用 where 条件?

来自分类Dev

如何在MySQL联接查询的一列中使用两个位置?

来自分类Dev

如何在MySQL中使用两个标签参数过滤结果?

来自分类Dev

如何在MySQL中使用两个条件进行内部联接

来自分类Dev

如何在带有case语句的mysql中使用两个小数点

来自分类Dev

如何在mysql中使用按日期顺序获取两个表的详细信息

来自分类Dev

如何在MySQL中使用INNER JOIN从两个表中删除行?

来自分类Dev

如何在复杂查询中使用 linq 将一个表连接到另外两个表?

来自分类Dev

如何在两个for循环中使用break?

Related 相关文章

  1. 1

    如何在Prometheus中使用两个指标执行查询?

  2. 2

    如何在MySQL中使用子查询显示两个表的混合结果?

  3. 3

    如何在MySQL中使用子查询显示两个表的混合结果?

  4. 4

    如何在VB中同时执行两个查询?(MySQL的)

  5. 5

    如何在一个查询中使用两个COUNT

  6. 6

    如何在PHP中使用一个mysqli查询更新两个选择框

  7. 7

    如何让两个promise同步执行?

  8. 8

    如何在C中使用open mp同时执行两个不同的功能

  9. 9

    如何在SQL中使用两个相似的参数执行搜索功能

  10. 10

    如何在bash中使用`fc`编辑和执行最后两个命令

  11. 11

    在mysql查询中使用两个内部联接

  12. 12

    在MySQL中使用子查询联接两个表

  13. 13

    如何在具有by和count()的SQL中使用两个查询的并集

  14. 14

    如何在SQL查询中使用两个联接?句法

  15. 15

    如何在PHP中使用不同的查询添加两个不同的行

  16. 16

    如何在React Native中使用两个Firestore查询获取数据

  17. 17

    如何在XSLT XPath查询中使用“两个自我”?

  18. 18

    如何在PHP中使用不同的查询添加两个不同的行

  19. 19

    如何在Codeigniter中使用单个查询在两个单独的表中插入数据?

  20. 20

    如何在 SQL 查询中使用两个 where 子句

  21. 21

    如何在 UNION 查询中的两个表中使用 where 条件?

  22. 22

    如何在MySQL联接查询的一列中使用两个位置?

  23. 23

    如何在MySQL中使用两个标签参数过滤结果?

  24. 24

    如何在MySQL中使用两个条件进行内部联接

  25. 25

    如何在带有case语句的mysql中使用两个小数点

  26. 26

    如何在mysql中使用按日期顺序获取两个表的详细信息

  27. 27

    如何在MySQL中使用INNER JOIN从两个表中删除行?

  28. 28

    如何在复杂查询中使用 linq 将一个表连接到另外两个表?

  29. 29

    如何在两个for循环中使用break?

热门标签

归档