简体   繁体   中英

Access to global variables inside Parse.Cloud.httpRequest{()}

{Edited my code to include parent loop)

I'm having trouble with the Parse.Cloud.httpRequest function that runs in Parse cloud code and there's no documentation on this method.

Essentially I would like to be able to either

  1. access a global variable (channel_id) with in the success part of Parse.Cloud.httpRequest({}) so that it can be passed as parameter into a function (DoSomething()) or
  2. get the JSON response from of Parse.Cloud.httpRequest({}) and move the function that uses it (DoSomething()) outside of Parse.Cloud.httpRequest({}) .

As of now, whatever variables I define inside of success have no scope outside of the function and when I try to access global variables inside success such as channel_id I have no access to them

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");
    }
});

Maybe there is a shorter version of the Parse.Cloud.httpRequest({}) function which simply takes the url and parameters etc. and returns the JSON or XML response?

In order to query multiple channel data you might create one scope for each request. eg 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..)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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