gpt4 book ai didi

javascript - 删除 Parse.com 中的所有旧对象

转载 作者:搜寻专家 更新时间:2023-11-01 00:32:57 25 4
gpt4 key购买 nike

我想清除超过一天的所有对象....所以我使用了下面给出的云代码。我的项目中有很多类,但以下代码仅适用于“消息”类。即,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!");
}
})
});

最佳答案

使用此代码非常有帮助。

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');
}
});
});

关于javascript - 删除 Parse.com 中的所有旧对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21912727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com