简体   繁体   English

for循环中具有可变URL的Javascript外部API调用

[英]Javascript external API calls with variable URL in for loop

I am trying to call the Twitch API in a for loop where the URL being called changes with each iteration through the loop and the URL that gets called is actually one that was returned in the previous call. 我正在尝试在for循环中调用Twitch API,其中被调用的URL随着循环的每次迭代而变化,被调用的URL实际上是在上一次调用中返回的URL。

Why? 为什么? - The Twitch API response limits you to 100 returned streams but includes a "next" link that sets the offset so that you can get the next 100 and then the next and so on. -Twitch API响应将您限制为返回100个流,但包含一个“下一个”链接来设置偏移量,以便您可以获取下100个,然后获取下一个,依此类推。

I am using meteor and wrote this: 我正在使用流星,并写道:

var extractGames = function(limit, pages) {
    var gamesURL = 'https://api.twitch.tv/kraken/streams';
    for (i=0; i < 3; i +=1) {
        Meteor.http.get(gamesURL, {
                params: {
                    limit: limit,
                    api_version: 3
                }
            }, function (err, res) {
                if (err) {
                    console.log(res.message);
                    console.log(res.error);
                } else {
                    gamesURL = res.data._links.next;
                    var data = res.data.streams;
                    console.log(gamesURL);
                    console.log(data[0].game);
                    console.log(data[75].game);
                    console.log("-----------");
                }

            }
        )
    }
};

I believe the problem is that the new URL is not getting called each time and rather the connection to the original is staying open and not changing. 我认为问题在于新URL不会每次都被调用,而与原始URL的连接保持打开状态并且不会改变。

The response looks like this: 响应如下所示:

? https://api.twitch.tv/kraken/streams?limit=100&offset=100
? H1Z1
? Pokémon Red/Blue
? -----------
? https://api.twitch.tv/kraken/streams?limit=100&offset=100
? H1Z1
? Pokémon Red/Blue
? -----------
? https://api.twitch.tv/kraken/streams?limit=100&offset=100
? H1Z1
? Pokémon Red/Blue
? -----------

How do I get the loop to call the new URL each time through? 如何获得每次通过循环调用新URL的循环?

Additionally, I have tried getting the total streams and dividing by 100 (+1) and doing it differently not using the "next" URL but that code was so messy compared to this - assuming I can get this to work. 另外,我尝试获取总流并除以100(+1),然后不使用“下一个” URL来进行其他处理,但是与之相比,该代码是如此混乱-假设我可以使它工作。

Thanks! 谢谢!

The problem here is that your requests are fired at the same time (with the same parameters!), and the variable is only changed when one returns, which is well after the first one returns. 这里的问题是您的请求在同一时间(使用相同的参数!)被触发,并且仅当一个返回时才更改该变量,这恰好在第一个返回之后。 This is because the Meteor function you called is asynchronous, and does not wait. 这是因为您调用的Meteor函数是异步的,并且不等待。 You could change it to synchronous, but I highly suggest against that because making it asynchronous is easily done. 您可以将其更改为同步,但我强烈建议您不要这样做,因为使其易于实现。

It looks something like 看起来像

A->http req A-> http请求

B->http req B-> http请求

C->http req C-> http请求

(wait) (等待)

A<-http return, sets var A <-http返回,设置var

C<-http return, sets var C <-http返回,设置var

C<-http return, sets var C <-http返回,设置var

You want to CHAIN them together, by making the next request call AFTER your first one returns, eg 您希望通过在您的第一个返回后拨打下一个请求来将它们链接在一起,例如

function DoMeteorCall() {
    Meteor.http.get(gamesURL, {
            params: {
                limit: limit,
                api_version: 3
            }
        }, function (err, res) {
            if (err) {
                console.log(res.message);
                console.log(res.error);
            } else {
                gamesURL = res.data._links.next;
                var data = res.data.streams;
                console.log(gamesURL);
                console.log(data[0].game);
                console.log(data[75].game);
                console.log("-----------");

                // do call here
                DoMeteorCall();
            }

        }
    )
}

This will ensure that AFTER the request returns, it will call another request with the new url. 这将确保在请求返回之后,它将使用新的URL调用另一个请求。

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

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