-1

大家,早安,

我是一名年轻的开发人员,我非常喜欢 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);
4

1 回答 1

0

试试这个

替换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);

于 2020-01-08T18:53:08.517 回答