【问题标题】:Parse background job not saving the objects解析后台作业不保存对象
【发布时间】:2014-08-19 13:43:30
【问题描述】:

我在 main.js 中编写了解析云作业,并正在使用 rest api 从外部站点获取数据并尝试将数据保存为解析对象,在日志中我看到循环正在运行,但每次只保存三个对象我运行后台作业的时间。

没有错误,作业状态每次都是成功。我试了几次,结果都是一样的。找不到相关文档,或者我可能在这里。保存对象的数量是否有任何限制,以及如何确保保存对象,因为我没有看到任何错误。

编辑:代码 - 网址已修改

Parse.Cloud.job("costCenterFeedJob", function(request, status) {
var auth = 'c2tvdG012313AjJA==';
Parse.Cloud.httpRequest({
  url: 'https://xxxx.xxxx?format=json',
  headers: {
    'Content-Type': 'application/json;charset=utf-8',
    'Authorization': 'Basic '+ auth 
  },
  success: function(httpResponse) {
   // console.log(httpResponse.text);
    console.log(httpResponse.data);

    var response = JSON.parse(httpResponse.text);
    var costCenters =  response.Report_Entry;
    console.log('json parse done');
    var WDCostCenter =  Parse.Object.extend("WDCostCenter");
    console.log(response.Report_Entry.length +'----');    
    var costCenter = null;   
    for(i=0; i<response.Report_Entry.length; i++) {
    costCenter = new WDCostCenter();
    costCenter.set("Name", response.Report_Entry[i].CostCenter);
    costCenter.set("RefID", response.Report_Entry[i].CostCenter_Workday_ID);
    costCenter.set("InActiveStatus", response.Report_Entry[i].InActive_Status);
    costCenter.save().then(function(message) {
    console.log("Success in Process :"+ i + message ); 
    }, function(error) {
       console.error("Error in Process :"+ i + error );
    });
    }
  },
  error: function(httpResponse) {
    console.error('Request failed with response code ' + httpResponse.status);
  }
}).then(function() {
    // Set the job's success status
    status.success("Migration completed successfully.");
  }, function(error) {
    // Set the job's error status
    status.error("Uh oh, something went wrong.");
  });

});

编辑:修改正确的代码 - 这有效

Parse.Cloud.job("costCenterFeedJob", function(request, status) {
var auth = 'c2t12313321==';
 var promises = [];
Parse.Cloud.httpRequest({
  url: 'https://xxxx.com?format=json',
  headers: {
    'Content-Type': 'application/json;charset=utf-8',
    'Authorization': 'Basic '+ auth 
  }
}).then(function(httpResponse) {
   var response = JSON.parse(httpResponse.text);
    var costCenters =  response.Report_Entry;
    console.log('json parse done');
    var WDCostCenter =  Parse.Object.extend("WDCostCenter");
    console.log(response.Report_Entry.length +'----');
    var costCenter = null;
    //var promises = [];
    for(i=0; i<response.Report_Entry.length; i++) {
        costCenter = new WDCostCenter();
        costCenter.set("Name", response.Report_Entry[i].CostCenter);
        costCenter.set("RefID", response.Report_Entry[i].CostCenter_Workday_ID);
        costCenter.set("InActiveStatus", response.Report_Entry[i].InActive_Status);
        promises.push(costCenter.save().then(
          function(message) {
            console.log("Success in Process :"+ i + message );
          }, function(error) {
                console.error("Error in Process :"+ i + error );
          })
        );
    }
    return Parse.Promise.when(promises);
    //console.success("httprequest  complented successfully.");
  }).then(function() {
    // Set the job's success status
    status.success("Migration completed successfully.");
  }, function(error) {
    // Set the job's error status
    status.error("Uh oh, something went wrong.");
  });
});

【问题讨论】:

  • 没有具体限制,可以贴一下相关代码吗?
  • 在描述中添加了代码。

标签: parse-platform cloud


【解决方案1】:

只要您调用 status.success/error,该作业就会终止。由于 .save() 是异步的,因此您需要确保您的保存也在 Promise 链中。

您可能需要进行更多调整,但将循环更改为如下所示

var promises = [];
for(i=0; i<response.Report_Entry.length; i++) {
    costCenter = new WDCostCenter();
    costCenter.set("Name", response.Report_Entry[i].CostCenter);
    costCenter.set("RefID", response.Report_Entry[i].CostCenter_Workday_ID);
    costCenter.set("InActiveStatus", response.Report_Entry[i].InActive_Status);

    promises.push(costCenter.save().then(
        function(message) {
            console.log("Success in Process :"+ i + message ); 
        },
        function(error) {
            console.error("Error in Process :"+ i + error );
        })
    );
}

return Parse.Promise.when(promises);

【讨论】:

  • 仍然看到相同的行为,我认为在 httprequest 根据日志成功完成之前,云作业已经完成。 “迁移成功完成首先打印”应该最后打印。 I2014-08-14T20:57:45.451Z] v46: Ran job costCenterFeedJob with: Input: {} 结果:迁移成功完成。[object Object] I2014-08-14T20:57:49.209Z] json 解析完成 I2014-08- 14T20:57:49.284Z] 276----
  • 汉克,我在原始帖子中添加了工作代码,循环执行在 then 函数中完成,而不是成功。感谢您指出 Promise 概念 :)
  • 这解决了我的问题。 status.success/error 不必在函数中。我只是删除了这些并且它起作用了。非常有帮助的回复,Parse 应该在他们的文档中添加这样的评论(或者如果他们已经这样做了,则放在更明显的地方)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-09
  • 2015-07-04
  • 2015-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多