Javascript 通过url和httpRequest解析云代码循环
我只想在我的“警报”对象中做一个简单的循环,其中包含一个url和一个单词。 对于每个警报,我都执行一个httpRequest来检查响应html代码中是否存在该单词。我是的,我把状态设置为真 我还想每次更新“updatedTo”列,即使在响应html代码中找不到该词,但我不知道为什么 我写了这个云代码,但它不起作用,或者它有时只在我只有单词present的项目时起作用Javascript 通过url和httpRequest解析云代码循环,javascript,parse-platform,Javascript,Parse Platform,我只想在我的“警报”对象中做一个简单的循环,其中包含一个url和一个单词。 对于每个警报,我都执行一个httpRequest来检查响应html代码中是否存在该单词。我是的,我把状态设置为真 我还想每次更新“updatedTo”列,即使在响应html代码中找不到该词,但我不知道为什么 我写了这个云代码,但它不起作用,或者它有时只在我只有单词present的项目时起作用 Parse.Cloud.job("updateStatus", function(request, status) {
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.");
});
});
查询。每个(回调、选项)都来自文档
迭代查询的每个结果,为每个结果调用回调。如果回调返回一个承诺,则迭代将不会继续,直到该承诺得到满足。如果回调返回一个被拒绝的承诺,那么迭代将因该错误而停止。项目按未指定的顺序处理。查询不能有任何排序顺序,也不能使用“限制”或“跳过”
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.");
});
});
因此,在任何帮助下,这都很困难,但我最终找到了另一个接近我需要的职位,我对其进行了调整,我成功地使用了它,它在承诺方面非常有效:):
嗨,非常感谢你的回答!我承认我是javascript的初学者,我很难理解什么是承诺。我阅读了文档,似乎Promise避免了一次调用就能获得成功的方法,但我在这里没有使用任何Promise。我在这里有点迷路了…我编辑了我的代码,但我真的不明白我能用它做什么,提前谢谢你的解释…有什么帮助吗?谢谢:)!
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);
});
});