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!");
}
})
});
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');
}
});
});
"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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.