{编辑我的代码以包含父循环)
我在使用 Parse 云代码中运行的 Parse.Cloud.httpRequest 函数时遇到问题,并且没有有关此方法的文档。
基本上我希望能够
- 使用 Parse.Cloud.httpRequest({}) 的 success 部分访问全局变量 (channel_id),以便可以将其作为参数传递到函数中(DoSomething()) 或
- 从 Parse.Cloud.httpRequest({}) 获取 JSON 响应,并将使用该响应的函数 (DoSomething()) 移至 Parse.Cloud.httpRequest({} )。
到目前为止,我在 success 内部定义的任何变量在函数之外都没有作用域,当我尝试访问 success 内部的全局变量(例如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 响应?
最佳答案
为了查询多个 channel 数据,您可以为每个请求创建一个范围。例如作者:
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 的内容放在循环内而不调用函数,就会发生这种情况。)
关于javascript - 访问 Parse.Cloud.httpRequest{()} 内的全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25894744/