Axios API返回Promise <pending>

时间:2020-01-23 18:13:32

标签: javascript node.js asynchronous promise axios

Axios处理似乎存在问题。下面的代码:

module.exports.SummonerRecentChampion = async event => {
  let {accountId, summonerName} = event.pathParameters;
  const enCodeSummonerName = encodeURI(summonerName);
  const getMatches = axios.get(`https://kr.api.riotgames.com/lol/match/v4/matchlists/by-account/${accountId}?endIndex=10&api_key=${api_key}&summonerName=${enCodeSummonerName}`);
  const championCount = (matches) => {
    return matches.reduce((b, c) => ((b[b.findIndex(d => d.champion.champion === c.champion)] || b[b.push({
      champion: c,
      count: 0
    }) - 1]).count++, b), []);
  };

  const championStats = (matches) => {
    return matches.map(match => {
      const setParticipant = [];
      return axios.get(`https://kr.api.riotgames.com/lol/match/v4/matches/${match.gameId}?api_key=${api_key}`).then(each => {
        const {participants, participantIdentities} = each.data;
        const participantId = participantIdentities.find(participant => participant.player.summonerName === summonerName).participantId;
        setParticipant.push(participants.find(participant => participant.participantId === participantId));
        return setParticipant
      })
    });
  };
  return getMatches.then(async fetchMatch => {
    const {matches} = fetchMatch.data;
    return Promise.all(
      [championCount(matches), championStats(matches)])
      .then(([championCount, championStats]) => {
        console.log('championCount:',championCount)
        console.log('championStats:',championStats)
        return {
          statusCode: 200,
          body: JSON.stringify(
            {
              data: championCount
            },
          )
        }
      })
  })
};

打击

[ Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> } ]

我认为我们在返回Axios时应该采取不同的方法,但是我不确定如何做。

还是不能在Axios中调用另一个API?

您能告诉我代码有什么问题吗?

0 个答案:

没有答案