【问题标题】:How to execute two mysql queries using promise in nodejs?如何在nodejs中使用promise执行两个mysql查询?
【发布时间】:2018-02-01 19:28:00
【问题描述】:

这是我要实现的目标,我希望根据第一个 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. product_id = 1
  2. product_name = MX-001

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

  1. product_id = 1
  2. file_name = maxgrand5.jpg
  3. file_path = 资产
  4. product_attachment_id = 1

一个 product_id 可以有多个 product_attachment_id

我怎样才能让数据加入?

我刚刚更新了我的问题,问题是我提出请求时第二次查询为时已晚,我应该使用promise让它不迟到,如何做到这一点?

【问题讨论】:

  • 您应该创建一个查询(可能是一个存储函数)并返回您需要的结果......
  • @UsagiMiyamoto 你有示例存储函数可以创建这样的 JSON 树吗?但我曾经读过 promise.all 可以做到这一点
  • @UsagiMiyamoto 嗨,我刚刚更新了关于承诺的问题
  • 你为什么不使用查询或存储过程,它正在连接两个表中的信息。有了它,您可以遍历该结果表的行...
  • @Myonara 你能告诉我一个存储过程查询示例来生成嵌套 JSON 吗?

标签: mysql arrays json node.js promise


【解决方案1】:

我用更简单的解决方案解决了这个问题,但这种方式不是完成它的“承诺”方式,所以如果你们遇到这样的问题,请决定使用哪个。因为我不需要很多数据来循环,只需要一个带有一维 JSON 数组的根数组 JSON,我就这么说吧:

app.post('/getsingleproduct', function (req, res) {
    var product_series = req.body.product_series;

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

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

            connection.query('call getProductAttachment("'+ rows[0][0].product_id +'")', function (err, rowsAttachment, fields) {
                if (!err) {

                    if (rowsAttachment.length != 0) {

                        response[0].data[0]['product_attachment']= rowsAttachment[0];

                        res.setHeader('Content-Type', 'application/json');
                        res.status(200).send(JSON.stringify(response));
                    } else {
                        response.push({ 'result': 'error', 'msg': 'No Results Found' });
                        res.status(400).send(err);
                    }
                }
            });

        } else {
            res.status(400).send(err);
        }
    });

});

这是一种非承诺方式,如果您需要承诺方式,请寻找@Myonara 的答案,我认为这是最好的

【讨论】:

    【解决方案2】:

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

    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...
    

    【讨论】:

    • 谢谢@Myonara,我想我可以在没有承诺的情况下做到这一点,我的代码可以在下面工作,但是因为我要求用“承诺”的方式来做 JSON 数组树,我有你的回答作为我的解决方案,非常感谢您
    猜你喜欢
    • 1970-01-01
    • 2018-01-21
    • 2015-08-30
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 2017-05-10
    相关资源
    最近更新 更多