如何遍历/等待axios?

我在遍历Ides时遇到问题,然后将结果推到上面的数组,它抛出此错误:“请求失败,状态码为404” 但是,如果我删除了循环,则无法正常工作

export default class GetPlayersPerMatch {
    constructor(numPlayers) {

        this.numPlayers = numPlayers;
        this.allPlayersMatchesArr = [];

    }
    async getResultsPerMatch() {
        try {
            const proxy = 'http://cors-anywhere.herokuapp.com/'

            for (let i = 0; i < 500; i++) {
                const resPerMatch = await axios(`${proxy}https://fantasy.premierleague.com/api/element-summary/${i}/`)
                this.playerData = resPerMatch.data
                console.log(this.playerData)
                this.allPlayersMatchesArr.push(this.playerData)
            }
            console.log(this.allPlayersMatchesArr) 

        } catch (error) {
            console.log(error)
        }
    }

}
WO129876SHI 回答:如何遍历/等待axios?

  1. 您如何确定要查看500页?
  2. 重构您的try-catch,以便您继续发送请求 失败的退货
  3. 当您说“如果我删除循环就可以了”,您的意思是 /api/element-summary/1/存在,但您的循环/请求可能会收到一个 索引15上的404或123或468 ...

尝试一下,让我们知道:

export default class GetPlayersPerMatch {
    constructor(numPlayers) {

        this.numPlayers = numPlayers;
        this.allPlayersMatchesArr = [];

    }
    async getResultsPerMatch() {
        const proxy = 'http://cors-anywhere.herokuapp.com/';

        for (let i = 0; i < 500; i++) {
            try {
                const resPerMatch = await axios(`${proxy}https://fantasy.premierleague.com/api/element-summary/${i}/`);
            } catch (error) {
                console.log(error);
            }
        this.playerData = resPerMatch.data;
        console.log(this.playerData);
        this.allPlayersMatchesArr.push(this.playerData);
        }
        console.log(this.allPlayersMatchesArr);        
    }
}
本文链接:https://www.f2er.com/3150519.html

大家都在问