2017-09-02 50 views
1

我有一個app.get會返回客戶數據和客戶購買。在這個app.get內部,我需要運行兩個mysql調用並構建一個數組來傳回。如何執行一個接一個的查詢並處理該數據?

如何執行一個接一個的查詢並處理該數據?

app.get('/customer', function (req,res) { 
    var response1 = []; 
    var response2 = []; 
    var processedData = []; 

     connection.query('QUERY HERE', function(err, rows, fields) { 
     if (!err){ 
       response.push({rows}); 
     } else { 
      res.status(400).send(err); 
     } 
    }); 

    //for loop 'response' results and perform another query 
    for (var i = 0; i < response1.length; i++) { 
     var row = response1[i]; 
     connection.query('QUERY HERE FOR row.customerid', function(err, rows, fields) { 
      if (!err){ 
       processedData.push({'Customer Name:' : row.customername, 'purchases' : rows}); 
      } else { 
       res.status(400).send(err); 
      } 
     }); 
     } 


    //Send json back 
    res.setHeader('Content-Type', 'application/json'); 
    res.status(200).send(JSON.stringify(processedData)); 

}); 
+0

添加您的循環和第二查詢,如果塊中。順便說一句,在JS中,我們通常會使用if(err){return res.status(400.send(err)},那麼在該塊之後的任何代碼都會在沒有錯誤時執行,並且不需要在塊內部。 ,如果你這樣做,你的循環和第二個查詢會在if塊之後出現 – Nocturno

+0

Hi @Nocturno,添加了我的第二個查詢以及for循環,語法可能不是100%,但是隻是想勾勒出我正在努力完成的任務 – user964627

+0

是的,我看到了你正在努力完成的任務,並且我想在別人試圖說服你使用複雜的異步庫之前向你展示標準的異步方式。你必須在第一個回調中添加你的循環和第二個查詢,這個回調只有在異步函數返回後纔會執行,這就是異步行爲的工作原理,我將重寫你的代碼並將其添加爲答案。 – Nocturno

回答

2

有一個名爲async.js提供做複雜的異步操作的一堆功能非常方便的模塊。特別是,

  • async.waterfall()是偉大的,當你需要從一個異步操作/任務結果傳下來的。

  • async.mapSeries()當你需要用一組異步操作/任務創建一個新數組時,它是非常棒的。

讓我們使用兩者。

如果我理解正確的代碼,該代碼將類似於東西

app.get('/customer', function (req, res) { 
    async.waterfall([ 
     // each task is passed a callback 'cb' as last argument; 
     // you MUST call it at least and at most once within each task; 
     // if you pass an error into the callback as the first argument, it will stop the async function 
     function task1 (cb1) { 
      //connection.query('QUERY HERE', function(err, rows, fields) { 
      // if (err) return cb1(err); // stop waterfall() if an error occurred 
      // cb1(null, rows, fields); // pass results down to next task 
      //}); 

      connection.query('QUERY HERE', cb1); // shorter version 
     }, 
     function task2 (rows, fields, cb2) { 

      // iterate and run async operation over each element in array 'rows' 
      async.mapSeries(rows, function getPurchases (row, cb3) { 
       connection.query('QUERY HERE FOR row.customerid', function (err, purchases, fields) { 
        if (err) return cb3(err); // stop mapSeries() if an error occurred 
        cb3(null, { 'Customer Name': row.customername, 'purchases': purchases }) 
       }); 
      }, function (err, customers) { 
       // when mapSeries() is done iterating OR if an error occurred, it will come here 

       if (err) return cb2(err); // stop waterfall() if an error occurred 
       cb2(null, customers) 
      }); 

     // }, cb2); // shorter version 

     } 
    ], function (err, customers) { 
     // when waterfall() is done all its tasks OR if an error occurred, it will come here 

     // handle error and send response here 
    }); 
}); 
相關問題