2016-10-03 9 views
0

私はnodejsとredis APIサービスを作成していますが、以下のコードを書いて日付範囲の個々の日付を見つけ出し、各日付のタイムスロットを取得してredisに格納します。strange for nodejsの各動作

問題は、forEach内でconsole.log("Keys :"+key)を実行し、同じforEach内のキーをチェックしていますが、何らかの理由でループが個々の関数を別々に実行しています。 Redisのをチェックするだけであるときに、キーの値を見ることができるように

私はより良い

//API to get slot for a particular date range 
app.get('/listcapacity/:ticketid/:fromdate/:todate', function(req, res) { 
    var id = req.params.ticketid; 
    var fromdate = req.params.fromdate; 
    var todate = req.params.todate; 
    var key = null; 
    var username = 'foo'; 
    var password = 'foobar'; 
    var result = {}; 
    var data_output = []; 
    var currentDate = new Date(fromdate); 
    var between = []; 
    var end = new Date(todate); 

    while (currentDate <= end) { 
     var tempdate = new Date(currentDate).toISOString(); 
     var dump = tempdate.toString().split("T"); 
     between.push(dump[0]); 
     currentDate.setDate(currentDate.getDate() + 1); 
    } 

    between.forEach(function(entry) { 
     key = id+entry+"list"; 
     console.log("Keys: " + key); 
     client.exists(key, function(err, reply) { 
      if (reply === 1) { 
       console.log("Key : " + key + " Found"); 
       client.get(key, function(err, reply) { 
        var output = JSON.parse(reply); 
        data_output = data_output.concat(output); 
       }); 
      } else { 
       console.log("Key : " + key + " Not Found"); 

       process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; 
       var slot_url = "https://" + username + ":" + password + "@testapi.foobar.com/1/timeslots?productId=" + id + "&fromDate=" + entry + "&toDate=" + entry; 
       request({ 

        url: slot_url, 
        json: true, 
        headers: headers 
       }, function(error, response, body) { 
        if (!error && response.statusCode === 200) { 
         var data = []; 
         try { 
          var temp = {}; 
          body.data.forEach(function(tempslots) { 
           temp['date'] = tempslots['date']; 
           temp['timeslots'] = tempslots['timeslots']; 
           data = data.concat(temp); 
          }); 
          client.set(key, JSON.stringify(data)); 
          data_output = data_output.concat(data); 
         } catch (err) { 

          console.log(err.message); 

         } 
        } else { 
         console.log("Something went wrong!! " + error.message); 
        } 
       }) 
      } 
     }); 
    }); 
    result['data'] = data_output; 
    result['response'] = 1; 
    result['message'] = 'Capacity list fetched successfully!'; 
    res.json(result); 

}); 

そして、ここでの問題を説明するためのコードと、コンソール出力を添付していますが、コンソール出力

Keys: 5212016-10-01list 
Keys: 5212016-10-02list 
Keys: 5212016-10-03list 
Keys: 5212016-10-04list 
Keys: 5212016-10-05list 
Key : 5212016-10-05list Not Found 
Key : 5212016-10-05list Not Found 
Key : 5212016-10-05list Not Found 
Key : 5212016-10-05list Not Found 
Key : 5212016-10-05list Found 

です最後の値を取得します。内部ではキーを定義するときに同じキーを定義すると、正しい値がコンソールに表示されます。

答えて

1

e redis操作は非同期です。コールバックは次のティックで実行されます。したがって、その時までにキー変数が最後の変数になります。

ローカル変数を使用する必要があります。