0
だから私は問題に遭遇しました。子関数から親関数に単一の文字列を渡す方法がわからず、その文字列をクライアント側への応答として渡します。Node.js親関数に変数を渡す
この全部で、最近の5試合がAPIから取得され、プレーヤー名に応じて勝敗が確認されます。
- 質問1:私は親の関数に子関数から文字列を渡した後、クライアント側への応答として送信する方法がわからない前に言ったように。
- 質問2:これの出力はWWWLWでなければならないと思います。しかし、それはLWWWW WLWWWのように異なる順序で出力するたびに...それは良い議論を持っていますが、異なる順序と私はここで何かを逃しています。
コード:
var request = require('request');
app.get('/history',getmatches, getwins);
function getmatches(req, res, next){
var match = {};
request({
url: "https://eun1.api.riotgames.com/lol/match/v3/matchlists/by-account/"+ID+"/recent?api_key=" + key,
json: true
}, function (error, res) {
if (!error && res.statusCode === 200) {
for(var i=0; i < 5; i++){ //getting ID's of five last matches
match[i] = res.body.matches[i].gameId;
}
req.somevariable = match;
next();
}
}
);
};
function getwins(req, res, callback){
var match = req.somevariable;
var streak = '';
var pending = 0;
for(i = 0; i < 5; i++){ // passing ID's to another api link to get single match data
request({
url: "https://eun1.api.riotgames.com/lol/match/v3/matches/"+match[i]+"?api_key=" + key,
json: true
}, function(req,res, body){
for(var j = 0; j < 10; j++){ //looping through 10 players in a match to find specific one
if(body.participantIdentities[j].player.summonerName == nickname){
if(body.participants[j].stats.win == true){
streak += 'W';
}else{
streak += 'L';
}
}
}
if(pending == 4){
console.log(streak); // need this to pass to parent function
return callback(null, streak); // is this something i need ?
}
pending++
});
}
// res streak string to client.js
};
JS非同期。ループが同じ順序で実行されるという保証はありません。要求をループ外の別の関数に移動し、 'i'を渡します。 –