Multiple Parse.Cloud.httpRequest in Parse.Cloud.job

827 views Asked by At

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");
  });
});

1

There are 1 answers

5
sarvesh On BEST ANSWER

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");
  });
});