2017-04-16 68 views
1

我正在使用expressJs來路由某些POST請求。 從客戶端傳遞對象的對象,並在服務器中使用for循環遍歷每個對象。陣列中的NodeJs項變量只取得for循環中的第一個值

我的問題,在循環變量cantidad只需要第一個值,而不是被刷新到pool.query,但需要正確的價值的pool.query之前。

所以,下面的行是確定的。

console.log("cantidad before query: " + cantidad); 

但是下面的線是壞的。它有第一個價值。

console.log("cantidad in query: " + cantidad); 

這是我的代碼的一部分。

for (var key in objects) { 
if (objects.hasOwnProperty(key)) { 
    ... 
    console.log("cantidad before query: " + cantidad); 
    pool.query(qProducto,idProducto, function (error, results, fields { 
    if (error) { 
    ... 
} else { 
    console.log("cantidad in query: " + cantidad); 
    ... 

這是ExpressJs完整的職位。

app.post("/commanda", function (req, res) { 
var idCuenta = req.body.idCuenta; 
var idEmpleado = req.body.idEmpleado; 
var fechaRegistro = req.body.fechaRegistro; 
var cuenta_mesero = "C:" + idCuenta + ":E:" + idEmpleado; 
var objects = req.body.objects; 
var element = {}; 
for (var key in objects) { 
    if (objects.hasOwnProperty(key)) { 
     var qProducto = "SELECT descripcionProducto FROM PRODUCTO WHERE idProducto = ? ;"; 
     var descProducto = ''; 
     console.log("cantidad in commanda2 : " + objects[key].cantidad); 
     try { 
      pool.query(qProducto, objects[key].idProducto, function (error, results, fields) { 
      if (error) { 
       console.error(error); 
       console.error("Failed with query: " + qProducto); 
       res.status(500).end(); 
       throw error; 
      } else { 
       console.log("cantidad in commanda4 : " + objects[key].cantidad); 
       descProducto = JSON.stringify(results[0].descripcionProducto); 
       element = { 
        idProducto:objects[key].idProducto, 
        cantidad:objects[key].cantidad, 
        descProducto:descProducto, 
        cuenta_mesero:cuenta_mesero, 
        fechaRegistro:fechaRegistro 
       }; 
       imprimirOrden(element); 
       } 
      }); 
     } catch (error) { 
     callback(error); 
     } 
    } 
} 
printer.printVerticalTab(); 
res.status(200).end(); 
}); 

這是一個物體的外觀。

{ '0': 
    { idProducto: '28', 
     cantidad: '3', 
     descProducto: 'Product1', 
     precioProducto: '3500', 
     precioTotal: 10500, 
    '$$hashKey': 'object:345' }, 
'1': 
    { idProducto: '29', 
     cantidad: '2', 
     descProducto: 'Product2', 
     precioProducto: '4500', 
     precioTotal: 9000, 
     '$$hashKey': 'object:346' } } 

回答

2

這是因爲功能for是同步的,但功能poll.query是異步的。這意味着使用for循環你基本上排隊一些查詢。你不是一個接一個地執行它們。因此,即使查詢返回一個結果,for循環也會完成。如果你想使用查詢中的數據進行下一次迭代,你應該開始使用async.js,這是一個npm模塊,可以幫助你避免這個問題。 TL;即使在一個查詢完成之前,您認爲在查詢中運行的控制檯日誌實際上也會運行。需要更多信息說明您聲明變量cantidad的位置,以及何時更改它以準確理解問題。

更新: 我剛纔告訴你的錯誤是因爲我錯誤地理解了else中的拘留。但是我已經告訴過你的確是問題所在。它很好地被混淆了。for循環在甚至一個查詢完成之前完成。他們只是排隊。所以第二個console.log將有循環中最後一個鍵的鍵。如果你需要知道你在哪個迭代中的邏輯,你應該實現一個異步函數,以便知道你實際在哪個迭代中。如果你不想使用異步庫,你可以使用這樣的東西。

首先在你的js的底部添加此功能的文件 https://pastebin.com/4tR0xaTY

你從根本上創造了環異步,你現在可以知道在哪個迭代您使用loop.iteration()。然後用下面寫的代碼替換你的郵政編碼(包括異步循環)。 https://pastebin.com/YzZU7bqp

+0

好吧,你有一個想法如何實施?使用async.parallel?系列?瀑布? eachSeries?實際上,我的變量'cantidad'不是查詢的結果,而是對象的'object'的一部分。查詢結果是另一個成功更新的變量。 – negrotico19

+0

您是否根據查詢結果更改變量cantidad?如果是,那麼我相信系列功能就是你要找的。它將確保在前一個查詢完成後調用每個查詢。 – itsundefined

+0

閱讀你的編輯評論後,我仍然不明白這個問題。代碼的哪一部分是你改變變量cantidad?因爲您聲明只有第一個值是控制檯記錄的。 – itsundefined

相關問題