2017-10-14 18 views
0

J'ai donc rencontré un problème. Je ne sais pas comment passer une chaîne unique à la fonction parent à partir d'une fonction enfant, puis passer cette chaîne en réponse au client.Node.js passant la variable à la fonction parente

Tout cela obtient cinq correspondances récentes de l'API, puis vérifie une victoire ou une perte en fonction du nom du joueur.

  • Question 1: comme je l'ai dit avant que je ne sais pas comment passer d'une chaîne fonction de l'enfant à la fonction parentale, puis l'envoyer en réponse à côté client.
  • Question 2: la sortie de ceci devrait être WWWLW et comment je pense qu'il devrait être commandé comme cela. Mais chaque fois qu'il sort dans un ordre différent comme LWWWW WLWWW et ainsi de suite ... il a de bons arguments mais un ordre différent et il me manque quelque chose ici.

Code:

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 asynchrone. Il n'y a aucune garantie que votre boucle fonctionnera dans le même ordre. Déplacez la requête dans une fonction séparée en dehors de la boucle, puis passez-y. –

Répondre

0

Il y a une solution pour traiter tous les résultats quand il fait. La variable résultat a tous les résultats utilisent n'importe quelle clé appropriée au lieu d'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 
};