2017-07-17 4 views
0

変数を設定したり、少なくとも非同期の滝で返すのに問題があります。私はあなたが非同期に戻ることはできませんが、私の変数であるjsonFinalのコールバックを行い、データの下の関数に入ります。変数async waterfallを設定するには?

function getIndividualMatchJSONObjHelper(matchData, matchParticipantData, indexIter) { 
var individualMatchURL = 'https://na1.api.riotgames.com/lol/match/v3/matches/' + matchData.matchID[indexIter] + '?api_key=' + API_KEY; 
var jsonFinal; 
async.waterfall([ 
    function(callback) { 
      request(individualMatchURL, function(err, response, body) { 
      if(!err && response.statusCode == 200) { 
       var json = JSON.parse(body); 
       for (var j = 0; j < 10; j++) { 
        if (matchData.championID[indexIter] == json['participants'][j].championId) { 
         jsonFinal = json['participants'][j]; 
         callback(null, jsonFinal); 
        } 
       } 
      } 
      else { 
       console.log(err); 
       } 
     }); 
    } 
], 
function(err, data) { 
    if(err) { 
     console.log(err); 
    } 
    else { 
     jsonFinal = data; 
    } 
}); 
console.log(jsonFinal); 
return jsonFinal; 
} 

jsonFinalを正しく返す関数を取得するにはどうすればよいですか?

答えて

1

コールバック内の変数は、非同期操作の場合と同様にのみ取得できます。したがって、コールバックを受け入れるように関数を変更してください。

function getIndividualMatchJSONObjHelper(matchData, matchParticipantData, indexIter, callback) { 
    var individualMatchURL = 'https://na1.api.riotgames.com/lol/match/v3/matches/' + matchData.matchID[indexIter] + '?api_key=' + API_KEY; 
    async.waterfall([ 
     function (callback) { 
      request(individualMatchURL, function (err, response, body) { 
       // always trigger the callback even when you have errors or else your program will hang 
       if (err) 
        return callback(err); 
       if (response.statusCode != 200) 
        return callback(new Error('Status code was ' + response.statusCode)); 

       var json = JSON.parse(body); 
       for (var j = 0; j < 10; j++) { 
        if (matchData.championID[indexIter] == json['participants'][j].championId) { 
         // match found: send back data 
         console.log('inside', json['participants'][j]); 
         return callback(null, json['participants'][j]); 
        } 
       } 
       // if it reaches this point, no match was found 
       callback(); 
      }); 
     } 
    ], callback); // note: this outer 'callback' is NOT the same as the inner 'callback' 
} 

次に、関数を呼び出したときなど。

getIndividualMatchJSONObjHelper(data, participants, indexIter, function (err, json) { 
    // you can only get the JSON at this point 
    console.log('outside', json); 
    matchParticipantData.specificParticipantData[i] = json; 
}); 
+0

返信いただきありがとうございます。しかし、問題は、変数に変数を格納すると、出力時に未定義になるということです。あなたはそのコールバックを返すと確信していますか?変数の格納方法は次のとおりです。 'matchParticipantData.specificParticipantData [i] = getIndividualMatchJSONObjHelper(matchData、matchParticipantData、i、function(err、json){}); ' –

+0

再度*コールバック内の変数は、任意の非同期操作で*。あなたの関数は何も返さないが、非同期操作が行われたときに引数 'json'を持つコールバックを提供する。編集を確認します。 – Mikey

+0

ありがとうございました!あなたはコールバックを少し良く理解するのを手伝ってくれました。しかし、私はただ一つの事を心配しています。私はオブジェクトを印刷することに気付いた。 'matchParticipantData.specificParticipantData [i]'は、関数の後にjsonを含んでいない。私は関数の中で変更された変数でテストしましたが、関数の外ではありません。これには理由がありますか? jsonが設定されている間、私はそれも関数の外にある必要があります –

関連する問題