Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何循环使用await axios?_Javascript_Loops_For Loop_Async Await_Axios - Fatal编程技术网

Javascript 如何循环使用await axios?

Javascript 如何循环使用await axios?,javascript,loops,for-loop,async-await,axios,Javascript,Loops,For Loop,Async Await,Axios,我在通过IDE循环时遇到了一个问题,然后将结果推送到上面的数组,它抛出了这个错误:“请求失败,状态代码404” 但是如果我去掉了这个循环,它就会正常工作 export default class GetPlayersPerMatch { constructor(numPlayers) { this.numPlayers = numPlayers; this.allPlayersMatchesArr = []; } async getRe

我在通过IDE循环时遇到了一个问题,然后将结果推送到上面的数组,它抛出了这个错误:“请求失败,状态代码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)
        }
    }

}
导出默认类GetPlayersPerMatch{
构造函数(numPlayers){
this.numPlayers=numPlayers;
this.allplayersmatchesar=[];
}
异步getResultsPerMatch(){
试一试{
常量代理服务器http://cors-anywhere.herokuapp.com/'
for(设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.allplayersmatchesar.push(this.playerData)
}
console.log(this.allPlayersMatchesArr)
}捕获(错误){
console.log(错误)
}
}
}
  • 你有多确定有500页要看
  • 重新构建try-catch,以便继续发送请求 在任何失败时返回
  • 当你说“如果我去掉这个循环,它就会工作”时,你的意思是
    /api/element summary/1/
    存在,但您的循环/请求可能会得到一个 索引15或123或468上的404
  • 像这样尝试,让我们知道:

    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);        
        }
    }
    
    导出默认类GetPlayersPerMatch{
    构造函数(numPlayers){
    this.numPlayers=numPlayers;
    this.allplayersmatchesar=[];
    }
    异步getResultsPerMatch(){
    常量代理服务器http://cors-anywhere.herokuapp.com/';
    for(设i=0;i<500;i++){
    试一试{
    const resPerMatch=await axios(`${proxy}https://fantasy.premierleague.com/api/element-summary/${i}/`);
    }捕获(错误){
    console.log(错误);
    }
    this.playerData=resPerMatch.data;
    console.log(this.playerData);
    this.allplayersmatchesar.push(this.playerData);
    }
    console.log(this.allPlayersMatchesArr);
    }
    }
    
  • 你有多确定有500页要看
  • 重新构建try-catch,以便继续发送请求 在任何失败时返回
  • 当你说“如果我去掉这个循环,它就会工作”时,你的意思是
    /api/element summary/1/
    存在,但您的循环/请求可能会得到一个 索引15或123或468上的404
  • 像这样尝试,让我们知道:

    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);        
        }
    }
    
    导出默认类GetPlayersPerMatch{
    构造函数(numPlayers){
    this.numPlayers=numPlayers;
    this.allplayersmatchesar=[];
    }
    异步getResultsPerMatch(){
    常量代理服务器http://cors-anywhere.herokuapp.com/';
    for(设i=0;i<500;i++){
    试一试{
    const resPerMatch=await axios(`${proxy}https://fantasy.premierleague.com/api/element-summary/${i}/`);
    }捕获(错误){
    console.log(错误);
    }
    this.playerData=resPerMatch.data;
    console.log(this.playerData);
    this.allplayersmatchesar.push(this.playerData);
    }
    console.log(this.allPlayersMatchesArr);
    }
    }
    
    如果你说的是
    ${proxy}https://fantasy.premierleague.com/api/element-summary/
    有效,但
    ${proxy}https://fantasy.premierleague.com/api/element-summary/${i}/
    没有,那么这不意味着你不应该通过
    i
    路径吗?你已经设置了该路径吗?元素摘要/1到元素摘要/500是否存在?如果你说的是
    ${proxy},我想出来了,谢谢你https://fantasy.premierleague.com/api/element-summary/
    有效,但
    ${proxy}https://fantasy.premierleague.com/api/element-summary/${i}/
    没有,那么这不意味着你不应该通过
    i
    路径吗?你已经设置好路线了吗?元素摘要/1到元素摘要/500是否存在?否则,你会得到404的那些不存在的我想出来了,谢谢盖伊虽然这是一个非常强大的解决方案和一种很好的做法这样做,但是您可以从i=1开始循环,因为我手动检查了链接,并且是/0/导致了此问题。我使用500检查它们是否为567实际上:D问题是i=0,尽管没有ID 0。非常感谢你,很高兴认识你,玩得开心!虽然这是一个非常健壮的解决方案,也是一种很好的实践,但您可以从i=1开始循环,因为我手动检查了链接,并且是/0/导致了此问题。我使用500检查它们是否为567实际上:D问题是i=0,尽管没有ID 0。非常感谢你,很高兴认识你,玩得开心!