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

时间:2015-06-10 18:30:20

标签: parse-platform httprequest cloud-code

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

  1. 从我的Parse.com课程中获取所有对象
  2. 使用Parse.com类中每个对象的值制作Parse.Cloud.httpRequest
  3. 处理Parse.Cloud.httpRequest响应并将其保存到我的Parse.com类
  4. 目前我可以从我的班级中检索所有对象(步骤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);
          }
      );
    });
    &#13;
    &#13;
    &#13;

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

    更新:

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

1 个答案:

答案 0 :(得分:1)

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