0

{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?

user3711987
  • 303
  • 2
  • 9

1 Answers1

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..)

fast
  • 885
  • 7
  • 15