0

{编辑我的代码以包含父循环)

我在 Parse 云代码中运行的 Parse.Cloud.httpRequest 函数遇到问题,并且没有关于此方法的文档。

本质上,我希望能够

  1. 在Parse.Cloud.httpRequest({})的成功部分访问全局变量 (channel_id),以便可以将其作为参数传递给函数 (DoSomething()) 或
  2. Parse.Cloud.httpRequest({})获取 JSON 响应并将使用它的函数 (DoSomething())移到 Parse.Cloud.httpRequest({})之外。

到目前为止,我在success内部定义的任何变量在函数之外都没有作用域,当我尝试访问成功内部的全局变量(例如 channel_id)时,我无法访问它们

var query = new Parse.Query("Channel");
query.equalTo("FrequentlyUpdated", false);
query.find ({
    success: function (results) {
        for (var i = 0; i < results.length; i++) {  

             channel_id = results[i].get("channel_id");               


               Parse.Cloud.httpRequest({
                  url: 'http://vimeo.com/api/v2/channel/' + channel_id + '/videos.json',
                     success: function (httpResponse) {
                     var response = httpResponse.text;
                     DoSomething(response, channel_id );
               },
               error: function (httpResponse) {
                status.error("failed");
               }
                });
         }
  },
    error: function() {
        status.error("movie lookup failed");
    }
});

也许有一个 Parse.Cloud.httpRequest({}) 函数的较短版本,它只接受 url 和参数等并返回 JSON 或 XML 响应?

4

1 回答 1

1

In order to query multiple channel data you might create one scope for each request. e.g. by:

var channels = [....];

for(var i=0; i < channels.length; i++) {
 queryChannel(channels[i], DoSomething);
}

function queryChannel(channel_id, onSuccess) {

 Parse.Cloud.httpRequest({
    url: 'http://vimeo.com/api/v2/channel/' + channel_id + '/videos.json',
    success: function (httpResponse) {
            var response = httpResponse.text;
            onSuccess(response, channel_id );
    },
    error: function (httpResponse) {
            status.error("failed");
    }
  });
}

Note that by calling queryChannel a new scope is introduced that preserves the channel_id from being overwritten by the next loop pass. (which woud happen if you place the contents of queryChannel just inside the loop, without the function invocation..)

于 2014-09-17T15:56:17.190 回答