Node.js将变量传递给父函数

时间:2017-10-14 15:09:24

标签: node.js function api request response

所以我遇到了一个问题。我不知道如何从子函数将单个字符串传递给父函数,然后将该字符串作为响应传递给客户端。

这整个事情从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
};

1 个答案:

答案 0 :(得分: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
};