如何使多个httprequest与分页游标在解析云

How to make multiple HttpRequests with pagination cursors in Parse Cloud

本文关键字:游标 分页 何使多 httprequest      更新时间:2023-09-26

我想从一个url做一个HTTP GET请求,它将包含下一个网页的url。我必须继续这个过程,直到我得到一个空的"next" url。

我的代码如下:
Parse.Cloud.define("myFunc", fucntion (request, response){
    Parse.Cloud.httpRequest({
      url: fb_url
    }).then(function(httpResponse) {
       next_url = httpResponse.data.next_url;
       /******************/
       // code to make another HttpRequest with next_url and iteratively 
       // doing it till next_url is null
        response.success(httpResponse.text);   
    }, function(httpResponse) {
        response.error("error " + httpResponse); 
    }
});
我尝试了很多不同的方法,但都是徒劳的。谁能告诉我如何与next_url做另一个HttpRequest,并继续这样做,直到next_url为空。

将http调用封装在一个可以递归调用的函数中。这将返回一个承诺链,这些承诺将发出请求,直到返回null。

function keepGetting(url) {
    return Parse.Cloud.httpRequest({ url:url }).then(function(httpResponse) {
        nextUrl = httpResponse.data.nextUrl;
        return (nextUrl === null)? httpResponse : keepGetting(nextUrl);
    });
}
Parse.Cloud.define("myFunc", fucntion (request, response){
    // initialize fb_url somehow
    keepGetting(fb_url).then(function(result) {
        response.success(result);   
    }, function(error) {
        response.error(error); 
    });
});

(注意,如果服务花费太长时间或在null之前返回太多结果,解析调用将超时)