2017-10-14 224 views
0

所以我遇到了一個問題。我不知道如何將單個字符串從子函數傳遞給父函數,然後將該字符串作爲響應傳遞給客戶端。Node.js將變量傳遞給父函數

這整個事情從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 
}; 
+0

JS異步。不能保證你的循環將以相同的順序運行。將請求移入循環外部的單獨函數,然後將'i'傳遞給它。 –

回答

0

有解決方案,以處理所有結果,當它完成。結果變量的所有結果都使用任何適當的鍵代替url;

function getwins(req, res, callback){ 
    var match = req.somevariable; 
    var streak = ''; 
    var pending = 0; 
    var results = {}; 
    var total = 5; 
    for(i = 0; i < total; i++){ // passing ID's to another api link to get single match data 
     var url = "https://eun1.api.riotgames.com/lol/match/v3/matches/"+match[i]+"?api_key=" + key; 
     request({ 
      url: url, 
      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'; 
         }   
        } 
       } 
       console.log(streak); // need this to pass to parent function  
       results[url] = streak; 
       if(total == Object.keys(results).length) { 
        // here all requests are done - do with all result what you need 
        console.log(results); 
       } 
       return callback(null, streak); // is this something i need ? 
      } 
     }); 
    } 
    // res streak string to client.js 
};