我正在制作(或嘲弄)一个允许英雄联盟玩家查找并比较自己和其他玩家的统计数据的网站。但是,我有一个问题,即在检索数据之前加载下一页。
我尝试将所有函数放在异步函数中,稍后再调用该函数。我也试图建立Promises,但我一直做错了,让自己感到困惑。
例如,我的两个主要功能是
function getSummonerID(summonerRegion, summonerName) {
return new Promise((resolve, reject) => {
var dataSummoner = {};
api.get(summonerRegion, 'summoner.getBySummonerName', summonerName)
.then((data) => {
if (data) {
dataSummoner.id = data.id;
dataSummoner.accountId = data.accountId;
dataSummoner.name = data.name;
dataSummoner.profileIconId = data.profileIconId;
dataSummoner.summonerLevel = data.summonerLevel;
dataSummoner.exists = true;
dataSummoner.region = summonerRegion;
console.log("\ngetSummonerID complete and exists\n");
} else {
dataSummoner.exists = false;
console.log("\ngetSummonerID complete and doesnt exist\n");
}
resolve(dataSummoner);
})
.catch(error => console.log(error));
})
}
和
function getHighestMastery(summonerRegion, summonerID) {
var dataMastery = {};
api.get(summonerRegion, 'championMastery.getAllChampionMasteries', summonerID)
.then((data) => {
dataMastery.championId = data[0].championId;
dataMastery.championLevel = data[0].championLevel;
dataMastery.championPoints = data[0].championPoints;
for (var i = 0; i < Object.keys(championJSON.data).length; i++)
if ((dataMastery.championId) === (championJSON.data[Object.keys(championJSON.data)[i]].id)) {
dataMastery.championName = championJSON.data[Object.keys(championJSON.data)[i]].name;
dataMastery.championTitle = championJSON.data[Object.keys(championJSON.data)[i]].title;
}
})
.catch(error => console.log(error));
console.log("\ngetHighestMastery complete\n");
return dataMastery;
}
我正在打电话给他们
async function retreiveData(summonerRegion, summonerName) {
summoner = await getSummonerID(summonerRegion, summonerName);
summoner.name = summonerName;
summoner.region = summonerRegion;
console.log("summoner");
console.log(summoner);
if (summoner.exists) {
mastery = await getHighestMastery(summonerRegion, summoner.id);
console.log("mastery");
console.log(mastery);
matches = await getRecentGames(summonerRegion, summoner.accountId, 10);
console.log("matches");
console.log(matches);
rankedInfo = await getRankedInfo(summonerRegion, summoner.id);
console.log("rankedInfo");
console.log(rankedInfo);
console.log("calls done");
}
return "done";
}
反过来,在这里打电话:
router.post('/summoner/submit', function(req, res, next) {
summoner.region = req.body.summRegion;
summoner.name = req.body.summName;
if (summoner.name) {
title = summoner.name + " on " + summoner.region + " - LOLSTATS.GG";
}
var x = retreiveData(summoner.region, summoner.name);
console.log("x is:");
console.log(x);
x.then((testVar) => {
console.log("test var: " + testVar);
setTimeout(function() {
console.log("summoner after 5s");
console.log(summoner);
console.log("mastery after 5s");
console.log(mastery);
console.log("matches after 5s");
console.log(matches);
console.log("rankedInfo after 5s");
console.log(rankedInfo);
}, 5000);
res.redirect('/summoner/lookup');
})
});
retrieveData中的console.logs变量AFTER console.log(召唤师)都是空的,但数据在“5s之后”部分,所以我知道我的功能正在运行。我很困惑,迷失了,对不起,我很抱歉。
答案 0 :(得分:0)
尝试将getHighestMastery
函数设为异步
async function getHighestMastery(summonerRegion, summonerID)
等待api.get()
来电。
var data = await api.get(summonerRegion, 'championMastery.getAllChampionMasteries', summonerID)
另外,请尝试添加
.catch(error => {
console.log(error));
reject(error);
}
到getSummonerID
函数