我在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.");
});
});
答案 0 :(得分:1)
一旦调用status.success / error,作业就会终止。由于.save()是异步的,因此您需要确保您的保存也在Promises链中。
你可能需要做一些调整,但是改变你的循环看起来像这样
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);