![](/img/trans.png)
[英]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.