Итак, небольшой контекст: я пытаюсь создать API, который подключается к Riot API и извлекает из него некоторые данные. Мне удалось сделать запросы, и «в теории» это работает благодаря водопаду.
, но я получаю крошечную ошибку: if (fn === null) выдает новую ошибку («Callback уже был вызван»)
Я не знаю, как это исправить, потому что я сделал этот код из примера кода, который использовал водопад ...
вот какой-то код:
var deaths = 0;
module.exports = function(app, api){
app.get("/",function(req, res){
var reqbody = req.body;
var s_search = reqbody.summoner;
var s_id = "";
var matchIdList = [];
var serverhost = reqbody.host;
var URLsummoner = serverhost + "/lol/summoner/v4/summoners/by-name/" + s_search + "?api_key=" + apikey;
var URLmatchlist = "";
var URLmatch = "";
async.waterfall([
function(callback){
request(URLsummoner, function(err, response, body){
if (!err && response.statusCode == 200) {
var json = JSON.parse(body);
console.log(json.name);
console.log(json.accountId);
s_id = json.accountId;
callback(null);
}else {
console.log(err);
}
});
},
function(callback){
URLmatchlist = serverhost + "/lol/match/v4/matchlists/by-account/" + s_id + "?endIndex=10&api_key=" +apikey;
request(URLmatchlist, function(err, response, body){
if (!err && response.statusCode == 200) {
var json = JSON.parse(body);
json.matches.forEach((match, i) => {
matchIdList.push(match.gameId);
console.log(match.gameId);
});
callback(null);
}else{
console.log(err);
}
});
},
function(callback){
for (matchId in matchIdList){
URLmatch = serverhost + "/lol/match/v4/matches/" + matchIdList[matchId] + "?api_key=" + apikey;
console.log(URLmatch);
request(URLmatch, function(err, response, body){
var matchJson = JSON.parse(body);
var participanteId = 0;
matchJson.participantIdentities.forEach((participante, i) => {
if (participante.player.summonerName == s_search) {
participanteId = participante.participantId;
}
});
matchJson.participants.forEach(function(participante, i) {
if (participante.participantId == participanteId) {
deaths += participante.stats.deaths;
}
});
console.log(deaths);
callback(null, deaths);
});
}
console.log(deaths);
}
],function(err, deaths){
if (err) {
console.log(err);
}
res.setHeader('Content-Type', 'application/json');
res.json(deaths);
});
});
};