簡體   English   中英

訪問Parse.Cloud.httpRequest {()}內部的全局變量

[英]Access to global variables inside Parse.Cloud.httpRequest{()}

{編輯我的代碼以包含父循環)

我在Parse雲代碼中運行的Parse.Cloud.httpRequest函數遇到麻煩,並且此方法沒有文檔。

本質上,我希望能夠

  1. Parse.Cloud.httpRequest({})成功部分中訪問全局變量(channel_id),以便可以將其作為參數傳遞給函數(DoSomething())或
  2. Parse.Cloud.httpRequest({})獲取JSON響應, 並將使用它的函數(DoSomething()) 移到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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM