Javascript 如何循环使用await axios?
我在通过IDE循环时遇到了一个问题,然后将结果推送到上面的数组,它抛出了这个错误:“请求失败,状态代码404” 但是如果我去掉了这个循环,它就会正常工作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
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(错误)
}
}
}
/api/element summary/1/
存在,但您的循环/请求可能会得到一个
索引15或123或468上的404export 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);
}
}
/api/element summary/1/
存在,但您的循环/请求可能会得到一个
索引15或123或468上的404export 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。非常感谢你,很高兴认识你,玩得开心!