简体   繁体   English

无法将匹配数据列表从 riot api 保存到 state 数组

[英]Having trouble saving a list of match data from riot api to a state array

Trying to create a small op.gg like app.尝试创建一个类似 op.gg 的小型应用程序。

Basically what im trying to do is to find a way of grabbing data from each match and being able to display it.基本上我想做的是找到一种从每场比赛中获取数据并能够显示它的方法。 I was able to grab that data and print it to the console but cannot find a way to store into a state array or have an efficient way to update the state array in order to display it in order onto the web.我能够获取该数据并将其打印到控制台,但无法找到存储到 state 数组的方法,也无法找到更新 state 数组以便按顺序显示到 web 的有效方法。

As you can see, I made an api call to grab a list of match id's which i then use to do another api call grab the data from each one of those matches.如您所见,我拨打了 api 电话以获取匹配 ID 列表,然后我使用它来执行另一个 api 电话以从每个匹配中获取数据。 I used a bottleneck since the rate limit is 10 request per second.我使用了一个瓶颈,因为速率限制是每秒 10 个请求。

   loadPlayerMatchData(puuid) {
        var API_KEY = MYRIOTAPIKEY
        const limiter = new Bottleneck({
            minTime:500
        })
        const throttleGetMatchData = limiter.wrap(getMatchesInformation)

        axios.get("https://americas.api.riotgames.com/tft/match/v1/matches/by-puuid/"+puuid+"/ids"+"?api_key=" + API_KEY).then((res) => {
            console.log(res.data)//match IDS

            res.data.map((id)=> {
                  
                throttleGetMatchData(id)//calling function with a bottleneck throttle
            })
        }).catch((errs)=> {
            console.log(err)
        })

        function getMatchesInformation(id) {
            axios.get("https://americas.api.riotgames.com/tft/match/v1/matches/"+ id +"?api_key=" + API_KEY).then((res) => {
                console.log(res.data)

                //PROBLEM STARTS HERE!!!!
                
                //******this.state.playerMatchInfo.push(res.data) <--- wont let me do this
               //return(res.data)<---- returns unfulfilled promises



            }).catch((err)=> {
                console.log(err)
            })
           
        }
        
    }

You can see that I'm doing all of this inside a react hook.你可以看到我在一个 React Hook 中完成了所有这些。 I cant seem to use state arrays either inside a local function (getMatchesInformation) within the hook.我似乎无法在挂钩内的本地 function (getMatchesInformation) 中使用 state arrays。

In conclusion im trying to see if i can pull off somehow saving data into an array like this.state.matches = []总之,我想看看我是否能以某种方式将数据保存到这样的数组中。state.matches = []

Would using redux help solve this issue?使用 redux 会帮助解决这个问题吗?

Your data needs time to come back.您的数据需要时间才能恢复。 You can use async-await to solve the problem您可以使用 async-await 来解决问题

async function getMatchesInformation(id) {
    const res = await axios.get("https://americas.api.riotgames.com/tft/match/v1/matches/"+ id +"?api_key=" + API_KEY)
    this.state.playerMatchInfo.push(res.data)
   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM