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

这里是我要实现的,我想有一个从我的node.js服务器返回的JSON数据是基于第一个mysql查询(数组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,但我想在这里构build的是一个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

我怎样才能得到数据join?

我只是更新了我的问题,问题是第二个查询太晚了,当我提出这个请求的时候,我应该用promise来让它不迟到,该怎么做?

首先,我创build了查询,其中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... 

我用简单的解决方法来解决这个问题,但这种方式并不是一个“承诺”的方法,所以如果你们面对这样的问题,那么决定使用哪一个。 由于我不需要很多数据循环,只有一个单一的根数组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的答案,我认为最好的