创建后台作业以删除最后一天的记录

时间:2014-12-05 10:37:04

标签: ios xcode parse-platform background-process

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

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!");         
                       }
                       })

            });

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

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:1)

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