javascript - Parse cloud code loop through url and httpRequest -



javascript - Parse cloud code loop through url and httpRequest -

i want simple loop in "alerts" objects, contains url, , word. each alert, httprequest check if word nowadays in response html code. yes, set status true.

i want update each time "updatedto" column, if don't find word in response html code, don't know why...

i wrote cloud code, don't works, or works if have items word present.

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 + " on : " + alerturl) promise = promise.then(function() { homecoming parse.cloud.httprequest({ url: alerturl, headers: { 'user-agent': 'mozilla/5.0 (macintosh; intel mac os x 10_10) applewebkit/600.1.25 (khtml, gecko) version/8.0 safari/600.1.25' }, }).then(function(httpresponse) { console.log("we succeded access website"); var htmlcode = httpresponse.text; if (htmlcode.indexof(alertwords) >= 0) { if (alertstatus == false) { alert.set("status", true); console.log("new status:true"); homecoming alert.save(); } } else { alert.set("status", false); console.log("new status:false"); //i updated "updatedto" field, doesn't work homecoming alert.save(); } // need homecoming promise here if non of above status meet. }, function(error) { console.error('request failed response code ' + httpresponse.headers.location); // need homecoming rejected promise here. } }); }); homecoming promise; }).then(function() { status.success('status updated'); // set job's success status }, function(error) { // set job's error status status.error("uh oh, went wrong."); }); });

the query.each(callback, options) documentation.

iterates on each result of query, calling callback each one. if callback returns promise, iteration not go on until promise has been fulfilled. if callback returns rejected promise, iteration stop error. items processed in unspecified order. query may not have sort order, , may not utilize limit or skip.

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 + " on : " + alerturl) homecoming parse.cloud.httprequest({ url: alerturl, headers: { 'user-agent': 'a user classic agent' }, success: function(httpresponse) { console.log("we succeded access website"); var htmlcode = httpresponse.text; if (htmlcode.indexof(alertwords) >= 0) { if (alertstatus == false) { alert.set("status", true); console.log("new status:true"); homecoming alert.save(); } } else { alert.set("status", false); console.log("new status:false"); //i updated "updatedto" field, doesn't work homecoming alert.save(); } // need homecoming promise here if non of above status meet. }, error: function(httpresponse) { console.error('request failed response code ' + httpresponse.headers.location); // need homecoming rejected promise here. } }); }).then(function() { status.success('status updated'); // set job's success status }, function(error) { // set job's error status status.error("uh oh, went wrong."); }); });

javascript parse.com

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -