Parse Cloud Code: delete all objects after request

Scenario

I have an app that allows users to create an account, but also allows the user to delete their account. After deleting their account, I have a cloud code feature that will delete all user created posts. The cloud code I'm using is ...

//Delete all User posts
Parse.Cloud.define("deletePosts", function(request, response) {

    var userID = request.params.userID;

    var query = new Parse.Query(Parse.Post);
    query.equalTo("postedByID", userID);
    query.find().then(function (users) {

        //What do I do HERE to delete the posts?

        users.save().then(function(user) {
        response.success(user);
        }, function(error) {
        response.error(error)
        });

    }, function (error) {

         response.error(error);

    });

});

      

Question

Once I make a request for all the user's posts, how do I delete them then ? (see //What do I do HERE?

)

+3


source to share


2 answers


Updates in the line below your comment "What should I do HERE ...":

NOTES:

  • You don't need to call save () method, so I took this.

  • This is, of course, just a matter of personal preference, but you can choose a parameter name that makes a little more sense than "users" since you are not really asking for users, but rather Messages (which are simply associated with the user).




Parse.Cloud.define("deletePosts", function(request, response) {
    var userID = request.params.userID;

    var query = new Parse.Query(Parse.Post);
    query.equalTo("postedByID", userID);
    query.find().then(function (users) {

        //What do I do HERE to delete the posts?
        users.forEach(function(user) {
            user.destroy({
                success: function() {
                    // SUCCESS CODE HERE, IF YOU WANT
                },
                error: function() {
                    // ERROR CODE HERE, IF YOU WANT
                }
            });
        });
    }, function (error) {
         response.error(error);
    });
});

      

+2


source


you can use

Parse.Object.destroyAll(users); // As per your code – what you call users here are actually posts

      

See: http://parseplatform.org/Parse-SDK-JS/api/classes/Parse.Object.html#methods_destroyAll



Also, consider using Parse.Cloud.afterDelete in Parse.User (if that's what you mean by "deleting an account") to perform such cleanups.

Oh, and to be complete you don't need a save () procedure after destroyAll ()

+6


source







All Articles