解析云代码:如何迭代httpRequest?

时间:2015-01-02 20:28:25

标签: facebook-graph-api parse-platform

我正在做的是从我的Parse数据库中加载ID,并为每个我想要调用httpRequest到Facebook Api的ID。问题是当没有response.success(它结束proccess)调用时,httpRequest似乎不起作用。

这是我到目前为止所做的:

Parse.Cloud.define("hello", function(request, response) {

    var query = new Parse.Query("Location");
    query.find({
        success: function(results) {
            console.log(results);
            for (var i = 0; i < results.length; ++i)
               var locationId = results[i].get("locationId");
               console.log(locationId);

               Parse.Cloud.httpRequest({
                 url: 'https://graph.facebook.com/v2.2/'+locationId+'/events?access_token='+accessToken,
                 success: function(httpResponse) {
                    console.log(httpResponse.data);
                    response.success("result");
                 },
                 error:function(httpResponse){
                    console.error(httpResponse.message);
                    response.error("Failed to login");
                 }
               });
...

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我不是我的javascript最多,但我很确定这是你正在寻找的概念。可能存在更好的解决方案。

var totalResults = results.length;
var completedResults = 0;
var completion = function() {
    // Because you're doing multiple requests, you'll need to figure out how you determine success/failure.  ie: Are any failures considered an overall failure? Do you want to pass a more detailed success?
    response.success("Finished");
};
for (var i = 0; i < totalResults; ++i)
       var locationId = results[i].get("locationId");
       console.log(locationId);
       Parse.Cloud.httpRequest({
         url: 'https://graph.facebook.com/v2.2/'+locationId+'/events?access_token='+accessToken,
         success: function(httpResponse) {
            completedResults++;
            console.log(httpResponse.data);                 
            if (completedResults == totalResults) {
                completion();
            }
         },
         error:function(httpResponse){
            completedResults++;
            console.error(httpResponse.message);                 
            if (completedResults == totalResults) {
                completion();
            }
         }
       });
}

// ...