2

I need to implement the following in my Parse.Cloud.Job:

  1. Get all objects from my Parse.com class
  2. Make Parse.Cloud.httpRequest using values of each one of the objects from my Parse.com class
  3. Process Parse.Cloud.httpRequest response and save it to my Parse.com class

Currently i can retrieve all objects from my class (step 1) but when i am trying to make httpRequest (step 2) the cloud job is 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; i < 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 with 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 someone help me with this 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 to process.");
    objectsArray = results;

  }).then(function(){
    for(var i = 0; i < countObjects; i++){
      var promises = [];
      console.log("i: " + 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");
  });
});
Danny
  • 682
  • 1
  • 11
  • 21

1 Answers1

1

You are calling status.success before your http requests complete. The code needs to wait until they are all completed. You save these promises in an array and wait before you call status.success. https://www.parse.com/docs/js/guide#promises-promises-in-parallel

Try the 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 i = 0; i < countObjects; i++){
          var promises = [];
          promises.push(Parse.Cloud.httpRequest({
            url: 'https://www.myApi.com/',
            params: {value: valueOne}
          })
          .then(function(httpResponse){
              //process your reponse here
              //for example if you want to insert some data to parse
              //here is how you would do 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");
  });
});
sarvesh
  • 2,743
  • 1
  • 20
  • 30
  • Thanks ! But in what part of this code i can process httpResponse ? – Danny Jun 11 '15 at 17:57
  • Not sure what you want to do with the response, I updated the sample to show an example of saving something from the response to parse. Hope that helps, good luck. – sarvesh Jun 12 '15 at 05:30
  • First i need to retrieve all objects from my parse class (step 1), with your code i can do it. The second step is make httpRequest and then process httpResponse from httpRequest (step 2). This is what i need for now. – Danny Jun 12 '15 at 22:31
  • Thanks for the update ! It's works perfectly but only for first object from my class, then the Job is finished. I have updated code in my question, please have a look also heare is my logs LOGS: There 140 objects to process. i: 0 promises Ran job myCloudJob with: Input: "{}" Result: Job completed – Danny Jun 14 '15 at 20:11
  • Your return Parse.Promise.when(promises); line is inside the for loop, move it outside the loop and it will work. – sarvesh Jun 14 '15 at 22:26