How can I replace specific objects in parse to obj

2019-09-02 23:01发布

问题:

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 a variable, also called "content". If the user's input value ("content") matches a "content" value already saved in parse.com, I want to replace the "content" value in parse.com with the variable saved in "newlocation" (also inputted by the user).

The replacing part is not working and I am getting the error "objects.set is not a function". What am I doing incorrectly and what can I change so that if the variable "content" matches a content value in parse.com, it is replaced with the variable saved in "newlocation"? Thank you in advance.

My code is shown below:

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

      } 

      $("#post-form-change").submit(function(event){
        event.preventDefault();

        var content = $("#post-original").val();
        var newlocation = $("#post-new").val();

        postsSameAs(content).then(function(objects){
            console.log("replacing " + JSON.stringify(objects));
            objects.set("newlocation", newlocation);  //should replace the "content" value in parse.com to the variable saved in "newlocation"

            return objects.save();
            window.alert("You have successfully replaced " + content + " to " + newlocation);
        }, function(error) {
            console.log("error " + JSON.stringify(error));
        });         

      });

回答1:

The problem with the code is that find() resolves as an array of matching objects. objects.set(... is no good because objects is an array. Do you expect only one Post from the query? In that case, replace query.find() with query.first()



回答2:

Try objects["newlocation"] = newLocation;