2014-10-19 57 views
0

我只是想在我的「alerts」對象中做一個簡單的循環,它包含一個url和一個單詞。 對於每個警報,我都會執行一個httpRequest來檢查該單詞是否出現在響應html代碼中。我是的,我把狀態變爲真實。通過url和httpRequest解析雲代碼循環

我也希望每一次更新「updatedTo」一欄,即使我沒有找到在響應HTML代碼的話,但我不知道爲什麼...

我寫了這個雲代碼,但它不起作用,或者它有時只有當我只有帶有單詞的項目時才起作用。

Parse.Cloud.job("updateStatus", function(request, status) { 
    Parse.Cloud.useMasterKey(); 
    var counter = 0; 
    var AlertItem = Parse.Object.extend("Alert"); 
    var query = new Parse.Query(AlertItem); 
    query.each(function(alert) { 
      var alertTitle = alert.get("title"); 
      var alertUrl = alert.get("url"); 
      var alertStatus = alert.get("status"); 
      var alertWords = alert.get("research"); 
      console.log("Alert : " + alertTitle + " - Check if : " + alertWords + " is on : " + alertUrl) 

      promise = promise.then(function() { 
        return Parse.Cloud.httpRequest({ 
         url: alertUrl, 
         headers: { 
          'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10) AppleWebKit/600.1.25 (KHTML, like Gecko) Version/8.0 Safari/600.1.25' 
         }, 
        }).then(function(httpResponse) { 
          console.log("We succeded to access to the website"); 
          var htmlCode = httpResponse.text; 
          if (htmlCode.indexOf(alertWords) >= 0) { 
           if (alertStatus == false) { 
            alert.set("status", true); 
            console.log("new status:true"); 
            return alert.save(); 
           } 
          } else { 
           alert.set("status", false); 
           console.log("new status:false"); 
           //I do this to updated the "updatedTo" field, but it doesn't work 
           return alert.save(); 
          } 
          // You need to return a Promise here if non of the above condition meet. 
         }, 
         function(error) { 
          console.error('Request failed with response code ' + httpResponse.headers.Location); 
          // You need to return a rejected promise here. 
         } 
        }); 
      }); 
     return promise; 
    }).then(function() { 
    status.success('Status updated'); 
    // Set the job's success status 
}, function(error) { 
    // Set the job's error status 
    status.error("Uh oh, something went wrong."); 
}); 
}); 

回答

0

因此,任何幫助,這是很難的,但我完成找到另一篇文章是誰靠近我需要的,我適應它,我成功使用它是什麼,它的偉大工程與承諾:):

var _ = require('underscore.js') 

Parse.Cloud.job("updateStatus", function(request, response) { 

var alerts = Parse.Object.extend("Alert"); 
var query = new Parse.Query(alerts); 
query.equalTo("status", false); 

query.find().then(function(alerts) { 

    var promise = Parse.Promise.as(); 

    _.each(alerts, function(alert) { 

     var alertUrl = alert.get("url"); 
     ... 

     promise = promise.then(function() { 
      return Parse.Cloud.httpRequest({ 
       url: alertUrl 
       }).then(function(httpResponse) { 
        ... 
       }, 
       function(error) { 
        ... 
       }); 
      }); 
    }); 

    return promise; 

}).then(function() { 
    response.success("All status updated with success !"); 
}, 
function (error) { 
    response.error("Error: " + error.code + " " + error.message); 
}); 
}); 
0

來自文檔的query.each(callback,options)。

迭代查詢的每個結果,爲每個結果調用回調。如果回調返回一個承諾,迭代將不會繼續,直到履行承諾。如果回調返回一個被拒絕的承諾,那麼迭代將停止並出現該錯誤。這些項目以未指定的順序處理。查詢可能沒有任何排序順序,可能不會使用限制或跳過。

Parse.Cloud.job("updateStatus", function(request, status) { 
    Parse.Cloud.useMasterKey(); 
    var counter = 0; 
    var AlertItem = Parse.Object.extend("Alert"); 
    var query = new Parse.Query(AlertItem); 
    query.each(function(alert) { 
     var alertTitle = alert.get("title"); 
     var alertUrl = alert.get("url"); 
     var alertStatus = alert.get("status"); 
     var alertWords = alert.get("research"); 
     console.log("Alert : " + alertTitle + " - Check if : " + alertWords + " is on : " + alertUrl) 


     return Parse.Cloud.httpRequest({ 
      url: alertUrl, 
      headers: { 
       'user-agent': 'A user classic agent' 
      }, 
      success: function(httpResponse) { 
       console.log("We succeded to access to the website"); 
       var htmlCode = httpResponse.text; 
       if (htmlCode.indexOf(alertWords) >= 0) { 
        if (alertStatus == false) { 
         alert.set("status", true); 
         console.log("new status:true"); 
         return alert.save(); 
        } 
       } else { 
        alert.set("status", false); 
        console.log("new status:false"); 
        //I do this to updated the "updatedTo" field, but it doesn't work 
        return alert.save(); 
       } 
       // You need to return a Promise here if non of the above condition meet. 
      }, 
      error: function(httpResponse) { 
       console.error('Request failed with response code ' + httpResponse.headers.Location); 
       // You need to return a rejected promise here. 
      } 
     }); 
    }).then(function() { 
     status.success('Status updated'); 
     // Set the job's success status 
    }, function(error) { 
     // Set the job's error status 
     status.error("Uh oh, something went wrong."); 
    }); 
}); 
+0

嗨,非常感謝您的回答!我承認我是JavaScript的初學者,我很難理解什麼是Promise。我閱讀了文檔,看起來Promise避免使用一次調用就能獲得成功的方法,但我在這裏沒有使用任何Promise。我有點迷失在這裏... – user2178964 2014-10-20 11:26:06

+0

我編輯我的代碼,但我真的不明白我能做什麼,在此先感謝您的解釋... – user2178964 2014-10-20 11:32:15

+0

任何幫助?謝謝 :) ! – user2178964 2014-10-22 11:41:30