NODE JS API 列表排名好友的 RIOT

NODE JS API RIOT for list rank friends

大家早上好,

我是一名年轻的开发人员,我非常喜欢 nodejs,我尝试通过创建小型个人应用程序来自学。所以我创建了一个使用 RIOT API 的应用程序来显示昵称、联赛及其积分、赢得或失去的积分。它对一个人来说效果很好,但是当我决定让它对几个人都起作用时,它就卡住了。网上都在讲异步,新手一头雾水,下面是我的代码,首先感谢大家的阅读,在此先感谢大家的帮助。

Ps:抱歉我英语不好 :D

var https = require('https');
var http = require('http');


const EU = 'https://euw1.api.riotgames.com/lol/league/v4/entries/by-summoner/';
    const NA1 = 'https://na1.api.riotgames.com/lol/league/v4/entries/by-summoner/';
    const URLKEY = '?api_key=';
    const Thomas = 'ZHd2VXBU5vuGumSIty1WXq5OqWu0XpAfXG8TyjIgDSwAGlU';
    const John = 'DkcxPRTDj1WDUMiOn_RN16AtGqNvKKig6SItWxoc3FhmIfQ';
    const MaximeG = 'LzIGCODKziH7E4QvOJ0cFHL2MJ3B00pukIShOH8qYemKlXs';

var server = http.createServer(function(req, res){
    let data = '';
    const key = '';

    // Doesn't work when you put multiple players :(
    const PLAYERID= [Thomas,John]; 

    PLAYERID.forEach(id => {
        console.log('Here the different id');
        console.log(id);
        console.log('continuation');

    https.get(EU + id + URLKEY + key,
    (rr)=>{
        rr.on('data', (chunk) => {
            console.log(chunk.toString());
            data += chunk;
});
        rr.on('end', () => { 
            let result = JSON.parse(data);
            console.log('result before json');
            console.log(result);
            console.log('result after parse');
            console.log(data);

            let output = [''];
            for(let i=0;i<result.length;i++){
                output += "Name : "+result[i].summonerName+ " League : "+result[i].queueType+"\n\tRank : "+result[i].tier+" "+result[i].rank+" "+result[i].leaguePoints+" LP Nombre de victoires : "+result[i].wins+" Nombre de defaites : "+result[i].losses+" \n";
        console.log(output);
            }
            res.end(output);
        });
    });
});
}).listen(8080);

试试这个

用您的密钥替换 const key


var https = require('https');
var http = require('http');


const EU = 'https://euw1.api.riotgames.com/lol/league/v4/entries/by-summoner/';
const NA1 = 'https://na1.api.riotgames.com/lol/league/v4/entries/by-summoner/';
const URLKEY = '?api_key=';
const Thomas = 'ZHd2VXBU5vuGumSIty1WXq5OqWu0XpAfXG8TyjIgDSwAGlU';
const John = 'DkcxPRTDj1WDUMiOn_RN16AtGqNvKKig6SItWxoc3FhmIfQ';
const MaximeG = 'LzIGCODKziH7E4QvOJ0cFHL2MJ3B00pukIShOH8qYemKlXs';


const key = 'RGAPI-f5c263a7-1137-4c1c-b9a7-4cdef0738fcd';

// Doesn't work when you put multiple players :(
const PLAYERID = [Thomas, John];
PLAYERID.forEach(id => {
    console.log('Here the different id');
    console.log(id);
    console.log('continuation');
    const responses = []
    https.get(EU + id + URLKEY + key, (rr) => {
        rr.on('data', (chunk) => {
            responses.push(JSON.parse(chunk));
        });
        rr.on('end', () => {
            let output = ['']
            responses.forEach((item) => {
                for (let i = 0; i < item.length; i++) {
                    output += "Name : " + item[i].summonerName + " League : " + item[i].queueType + "\n\tRank : " + item[i].tier + " " + item[i].rank + " " + item[i].leaguePoints + " LP Nombre de victoires : " + item[i].wins + " Nombre de defaites : " + item[i].losses + " \n";
                    console.log(output);
                }
            })


        });
    });
});

var server = http.createServer(function (req, res) {

}).listen(8080);