2017-06-07 7 views
0

一連のデータベース呼び出しを行い、それらがすべて実行されたら何かを戻したいと思っています。参照のエラーを避けるために各メソッドを非同期で使用する正しい方法

私はこれを助け、配列の各要素に関数を適用するために非同期モジュールを使用しています。

async.each(items, sqlCall(), 
    function(err) { 
     if(err) { 
      // All processing will now stop. 
      console.log('A call failed'); 
     } else { 
      console.log("finished async operations"); 
     } 
    }); 

sqlCall = function(item , callback){ 
     console.log("arr is " + JSON.stringify(item)); 
     lampInfo = item[0]; 
     userRoom = item[1]; 
     sql = item[2]; 
     console.log('sql is ' + sql); 
     connection.query(sql, function(error, results, fields) { 
      if (error){ 
       console.log("why error"); 
       callback(error); 
      } 
      else { 
       console.log("successful call"); 
       lampSavingsStatement[savingsCalc(lampInfo, results[0], energyPrice)] = userRoom; 
       callback(); 
      } 
     }); 
    } 

lampSavingsStatementがグローバル辞書であるとsavingsCalcは私が何をARRが未定義の参照エラーを得続ける、と確認していない別の方法

であるが、以下のように私はSQLCallの定義問題は。あなたが機能を、sqlCall()async.eachに結果を渡していない

[ [ { lampName: '2ft T5 14W', number: '12', hours: '3' }, 
    'room2', 
    'SELECT * FROM `lightstoled` WHERE `techid` = \'2ft T5 14W\'' ] ] 

答えて

1

何項目の例

です。

が、この代わりに試してみてください:

async.each(items, sqlCall, function (err) { ... }); 
+0

括弧のペアが私を引き起こしたどのくらいのトラブルアメージング。ありがとう! – Abe

関連する問題