简体   繁体   中英

How can I delete specific rows in parse.com?

I have objects in Parse called "Post" and within that, I have columns called "title" and "content". I am trying to ask the user for an input value and save this as "remove". If the user's input value ("remove") matches a "title" value already saved in parse.com, I want to delete the entire row in parse, so that both the "title", "content" and everything else in the row is deleted. The deleting part is not working so I am wondering if my code is actually making it go through all the data saved in parse and find the one that matches the user's input and then delete it. What am I doing incorrectly and what can I change to make it delete the entire row? Thank you in advance.

function getPosts(){
        var query = new Parse.Query(Post);
        query.find({
          success: function(results){
            for(var i in results){
                var title = results[i].get("title");
                var content = results[i].get("content");

                var remove = $("#post-remove").val();
                console.log("Remove: "+remove);
                console.log("MAC Address: " +title);
                console.log("place: "+content);

                if (title == remove)
                {
                    window.alert("The MAC address matches.");
                    console.log(remove+" matches " + title+ " and is located in " +content);
                    var Post = Parse.Object.extend("Post");
                    var query = new Parse.Query(Post);
                    query.find("objectId", {
                      success: function(yourObj){
                        //console.log(yourObj);
                        //Post.destroy({});        //if title matches remove, delete the Post (title and content) (but it's not deleting it)

                        Post.remove("title");
                        Post.remove("content");

                      }
                    });
              }
            }
          }
        });
      }            

First of, you can use the Parse.Query.equalTo(key, value) method to filter for the Post/s you are looking for. That will render a lot of your logic unnecessary.

Additionally, since most parse calls are asynchronous, I would suggest learning about Parse Promises and using those instead of the call backs you're using.

Finally, you don't need a second nested query, since you already have the object you are trying to destroy. You just need to call destroy() on that object, and if you have some extra content you need to take care of deleting (ie, your 'content' is a pointer to another object that is owned only by the Post you are deleting), you should set up a beforeDestroy() trigger for the Post object in your cloud code that will delete that pointer as well.

To clarify and add a bit to @JakeT's acceptable answer:

1) find objects to delete like this:

function postsMatching(title) {
    var Post = Parse.Object.extend("Post");
    var query = new Parse.Query(Post);
    query.equalTo("title", title);
    return query.find();
}

2) Delete an array of parse objects like this:

Parse.Object.destroyAll(posts);

3) Put the two ideas together (returning a promise to find then delete) like this:

var title = $("#post-remove").val();
postsMatching(title).then(function(posts) {
    console.log("deleting " + JSON.stringify(posts));
    Parse.Object.destroyAll(posts);
}, function(error) {
    console.log("error " + JSON.stringify(error));
});

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM