2017-10-14 225 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 
};