Parse.Cloud.job 中的多个 Parse.Cloud.httpRequest

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

我需要在 Parse.Cloud.Job 中实现以下内容:

  1. 从我的 Parse.com class
  2. 获取所有对象
  3. 使用我的 Parse.com class
  4. 中每个对象的值制作 Parse.Cloud.httpRequest
  5. 处​​理 Parse.Cloud.httpRequest 响应并将其保存到我的 Parse.com class

目前我可以从我的 class 中检索所有对象(第 1 步),但是当我尝试创建 httpRequest(第 2 步)时,云作业已完成。

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

有人可以帮我完成这个任务吗?

更新:

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

您在 http 请求完成之前调用 status.success。代码需要等到它们全部完成。您将这些承诺保存在一个数组中,并在调用 status.success 之前等待。 https://www.parse.com/docs/js/guide#promises-promises-in-parallel

试试下面的代码

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