我只是想在我的“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.");
});
});
嗨,非常感谢您的回答!我承认我是JavaScript的初学者,我很难理解什么是Promise。我阅读了文档,看起来Promise避免使用一次调用就能获得成功的方法,但我在这里没有使用任何Promise。我有点迷失在这里... – user2178964 2014-10-20 11:26:06
我编辑我的代码,但我真的不明白我能做什么,在此先感谢您的解释... – user2178964 2014-10-20 11:32:15
任何帮助?谢谢 :) ! – user2178964 2014-10-22 11:41:30