10

I want to update object i already have in parse.com with javascript; what i did is i retirevied the object first with query but i dont know how to update it.

here is the code i use, whats wrong on it?

var GameScore = Parse.Object.extend("Driver");
var query = new Parse.Query(GameScore);
query.equalTo("DriverID", "9");
query.find({
  success: function(results) {

    alert("Successfully retrieved " + results.length + "DName");

     results.set("DName", "aaaa");
    results.save();
  },
  error: function(error) {
    alert("Error: " + error.code + " " + error.message);
  }
});
1
  • looks fine according to the documentation, what is the actual problem you are having? Commented Nov 6, 2012 at 13:24

5 Answers 5

22

The difference between the question and your answer may not be obvious at first- So for everyone who has happened here- Use query.first instead of query.find.

query.find()  //don't use this if you are going to try and update an object

returns an array of objects, an array which has no method "set" or "save".

query.first() //use this instead

returns a single backbone style object which has those methods available.

Sign up to request clarification or add additional context in comments.

Comments

16

I found the solution, incase someone needs it later

here it is:

var GameScore = Parse.Object.extend("Driver");
var query = new Parse.Query(GameScore);
query.equalTo("DriverID", "9");
query.first({
  success: function(object) {

     object.set("DName", "aaaa");
    object.save();


  },
  error: function(error) {
    alert("Error: " + error.code + " " + error.message);
  }
});

3 Comments

The difference between the question and your answer may not be obvious at first- So for everyone who has happened here- Use query.first instead of query.find. Find returns an array of objects, and the array has no method "set" or "save". First returns a single backbone style object which has those methods available.
how can i do both save or update in one function. Set is for only update the rows. If row is not exist how to save inside the find function
I'm able to retrive my values using objectId , but on success i try to set a new value , but always i get "{"code":101,"error":"object not found for update"}" error , please help me . Thanks In advance :)
3

If someone got msg "{"code":101,"error":"object not found for update"}", check the class permission and ACL of Object to enrure it's allowed to read and write

Comments

1

Do something like this:

var GameScore = Parse.Object.extend("Driver");
var query = new Parse.Query(GameScore);
query.equalTo("DriverID", "9");
query.find({
  success: function(results) {

   alert("Successfully retrieved " + results.length + "DName");
      // - use this-----------------
      results.forEach((result) => {
        result.set("DName", "aaaa");
      });
      Parse.Object.saveAll(results);
      // --------------------------------
 },
 error: function(error) {
   alert("Error: " + error.code + " " + error.message);
 }
});

Comments

0

You can do it like this:

 var results= await query.find();
          for (var i = 0; i < results.length; i++) {
          results[i].set("DName", "aaaa");
          results[i].save();
          }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.