parse.com - Multiple Parse.Cloud.httpRequest in Parse.Cloud.job -
i need implement following in parse.cloud.job:
- get objects parse.com class
- make parse.cloud.httprequest using values of each 1 of objects parse.com class
- process parse.cloud.httprequest response , save parse.com class
currently can retrieve objects class (step 1) when trying make httprequest (step 2) cloud job finished.
parse.cloud.job("mycloudjob", function(request, status){ var countobjects; var objectsarray = []; var query = new parse.query("myclass"); query.limit(200); query.find().then(function(results){ countobjects = results.length; objectsarray = results; }).then(function(){ for(i = 0; < countobjects; i++){ var valueone = objectsarray[i].attributes.valueone; parse.cloud.httprequest({ url: 'https://www.myapi.com/', params: {value: valueone} }).then(function(httpresponse) { console.log(httpresponse.status); response.success('status: ' + httpresponse.status + 'response: ' + httpresponse.text); }, function(httpresponse) { console.error('request failed response code ' + httpresponse.status); response.error('error, status ' + httpresponse.status + ' data' + httpresponse.text); }); } status.success("there " + countobjects + " objects in myclass."); }, function(error) { status.error("error: " + error.code + " " + error.message); } ); });
can me task please ?
update:
parse.cloud.job("mycloudjob", function(request, status){ var countobjects; var objectsarray = []; var query = new parse.query("myclass"); query.limit(200); query.find().then(function(results){ countobjects = results.length; console.log("there " + countobjects + " objects process."); objectsarray = results; }).then(function(){ for(var = 0; < countobjects; i++){ var promises = []; console.log("i: " + + " promises " + promises); var objectid = objectsarray[i].id; var myvalue = objectsarray[i].attributes.value; promises.push(parse.cloud.httprequest({ url: 'https://www.myapi.com', params: {value: myvalue} }) .then(function(httpresponse){ var datafromresponse = httpresponse.data; var point = parse.object.extend("myclass"); var point = new point(); point.id = objectid; point.set("value", datafromresponse); return point.save(); })); return parse.promise.when(promises); } }).then(function(){ status.success("job completed"); },function(){ status.error("error running job"); }); });
you calling status.success before http requests complete. code needs wait until completed. save these promises in array , wait before call status.success. https://www.parse.com/docs/js/guide#promises-promises-in-parallel
try following code
parse.cloud.job("mycloudjob", function(request, status){ var countobjects; var objectsarray = []; var query = new parse.query("myclass"); query.limit(200); query.find().then(function(results){ countobjects = results.length; objectsarray = results; }).then(function(){ for(var = 0; < countobjects; i++){ var promises = []; promises.push(parse.cloud.httprequest({ url: 'https://www.myapi.com/', params: {value: valueone} }) .then(function(httpresponse){ //process reponse here //for example if want insert data parse //here how it. var myobject2 = parse.object.extend("object2"); myobject2.set("prop1",httpresponse.data.propertyname); return myobject2.save(); })); return parse.promise.when(promises); } }).then(function(){ status.success("job completed"); },function(){ status.error("error running job"); }); });
Comments
Post a Comment