[英]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. 我在Parse云代码中运行的Parse.Cloud.httpRequest函数遇到麻烦,并且此方法没有文档。
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 到目前为止,无论我在成功内部定义的变量在函数外部都没有作用域,并且当我尝试在成功内部访问全局变量(例如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");
}
});
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? 也许有一个Parse.Cloud.httpRequest({})函数的较短版本,该函数仅获取url和参数等并返回JSON或XML响应?
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. 注意,通过调用queryChannel,引入了一个新的作用域,该作用域可防止channel_id被下一次循环遍历覆盖。 (which woud happen if you place the contents of queryChannel just inside the loop, without the function invocation..)
(如果将queryChannel的内容放在循环内部,而没有调用函数,则会发生这种情况。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.