Delete all old objects in Parse.com

寵の児 提交于 2019-12-23 23:16:00

问题


I wanted to clear all objects which a more than one day old....so I used the below given cloud code. There are many classes in my project but the below code only works for class 'Messages'. ie., the class name mentioned in Parse.Object.extend("Messages").

I wanted to loop through all classes in my project and delete data which is more than 1 day old. How can I iterate through all classes in my project and run the same code for each class?

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

回答1:


use this code its very helpful.

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



回答2:


"You can only perform a limited amount of asynchronous operations in Cloud Code. By calling destroy() repeatedly from a loop, you're likely to run into this restriction." - Hector From Parse

I would suggest first converting your cloud code to a "job" so your timeout is 15 minutes vs 15 seconds, then replace destroy() with:

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.
    }

});

And wait to continue your deletion until you have received the callback.



来源:https://stackoverflow.com/questions/21912727/delete-all-old-objects-in-parse-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!