[英]Calling Parse.Cloud.httpRequest inside another httpRequest
[英]Access to global variables inside Parse.Cloud.httpRequest{()}
{编辑我的代码以包含父循环)
我在Parse云代码中运行的Parse.Cloud.httpRequest函数遇到麻烦,并且此方法没有文档。
本质上,我希望能够
到目前为止,无论我在成功内部定义的变量在函数外部都没有作用域,并且当我尝试在成功内部访问全局变量(例如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响应?
为了查询多个渠道数据,您可以为每个请求创建一个范围。 例如:
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");
}
});
}
注意,通过调用queryChannel,引入了一个新的作用域,该作用域可防止channel_id被下一次循环遍历覆盖。 (如果将queryChannel的内容放在循环内部,而没有调用函数,则会发生这种情况。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.