我想要清除一天以上的所有对象....所以我使用了下面给出的云代码。 我的项目中有许多类,但下面的代码仅适用于“消息”类。 即。,Parse.Object.extend(“Messages”)中提到的类名。
我想遍历项目中的所有类并删除超过1天的数据。 如何遍历项目中的所有类并为每个类运行相同的代码?
// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:
Parse.Cloud.job("deleteOldEntries", function(request, status) {
var yourObject = Parse.Object.extend("Messages");
var query = new Parse.Query(yourObject);
var day = new Date();
day.setDate(day.getDate() - 1);
query.lessThan("createdAt", day);
query.find
({
success:function(results)
{
for (var i = 0, len = results.length; i < len; i++)
{
var result = results[i];
result.destroy();
console.log("Destroy: "+i);
}
status.success("Delete successfully.");
},
error: function(error)
{
status.error("Uh oh, something went wrong.");
console.log("Failed!");
}
})
});
答案 0 :(得分:3)
使用此代码非常有用。
Parse.Cloud.job("deleteMessages", function(request, status)
Parse.Cloud.useMasterKey();
var ts = Math.round(new Date().getTime() / 1000);
var tsYesterday = ts - (24 * 3600);
var dateYesterday = new Date(tsYesterday*1000);
var query = new Parse.Query("Your Object Class");
query.lessThan("createdAt", dateYesterday);
query.find({
success: function(result) {
for(var i=0; i<result.length; i++) {
result[i].destroy({
success: function(object) {
status.success("Delete job completed");
alert('Delete Successful');
},
error: function(object, error) {
status.error("Delete error :" + error);
alert('Delete failed');
}
});
}
status.success("Delete job completed");
},
error: function(error) {
status.error("Error in delete query error: " + error);
alert('Error in delete query');
}
});
});
答案 1 :(得分:1)
“您只能在Cloud Code中执行有限数量的异步操作。通过从循环中反复调用destroy(),您可能会遇到此限制。” - Hector From Parse
我建议先将您的云代码转换为“作业”,这样您的超时时间为15分钟对15秒,然后将destroy()替换为:
myObject.destroy({
success: function(myObject) {
// The object was deleted from the Parse Cloud.
},
error: function(myObject, error) {
// The delete failed.
// error is a Parse.Error with an error code and description.
}
});
等待继续删除,直到收到回叫为止。