OStack程序员社区-中国程序员成长平台

标题: ios - 创建用于删除最后一天记录的后台作业 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 03:45
标题: ios - 创建用于删除最后一天记录的后台作业

如何从表格中删除最后一天的记录

Parse.Cloud.job("deleteOldEntries", function(request, status) {
            var GameScore = Parse.Object.extend("GameScore");
            var query = new Parse.Query(GameScore);
            var day = new Date();
            day.setDate(day.getDate()-1);

            query.lessThan("createdAt", day);

            query.find({
                       success:function(results) {
                       console.log(results);
                       for (var i = 0, len = results.length; i < len; i++) {
                       var result = results[i];

                       result.destroy({});
                       console.log("Destroy: "+result);

                       }
                       status.success("Delete successfully.");             
                       },
                       error: function(error) {
                       status.error("Uh oh, something went wrong.");
                       console.log("Failed!");         
                       }
                       })

            });

我正在使用此代码删除最后一天的记录,它已成功部署到服务器。之后我成功安排了作业,但记录不删除。

任何帮助表示赞赏。



Best Answer-推荐答案


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

关于ios - 创建用于删除最后一天记录的后台作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27313958/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4