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