【发布时间】:2017-08-05 09:49:07
【问题描述】:
我无法将 JSON 存储在变量中。当我调用 getIndividualMatchJSONObjHelper 函数时,json 变量本身会打印出来,但在变量外部不存储任何内容。如何在 matchParticipantData.specificParticipantData 中正确存储 JSON 变量?
function getIndividualMatchJSONObj(matchData) {
var matchParticipantData = {
specificParticipantData: [numberOfGames]
};
for (var i = 0; i < numberOfGames; i++) {
getIndividualMatchJSONObjHelper(matchData, matchParticipantData, i, function(err, json) {
matchParticipantData.specificParticipantData[i] = json;
});
}
return matchParticipantData;
}
function getIndividualMatchJSONObjHelper(matchData, matchParticipantData, indexIter, callback) {
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)
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) {
return callback(null, json['participants'][j]);
}
}
});
}
], callback);
}
【问题讨论】: