【发布时间】: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