Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i update current object in parse.com with javascript?

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);
  }
});
like image 445
AzabAF Avatar asked Nov 06 '12 13:11

AzabAF


4 Answers

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.

like image 200
Hairgami_Master Avatar answered Nov 11 '22 02:11

Hairgami_Master


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);
  }
});
like image 41
AzabAF Avatar answered Nov 11 '22 01:11

AzabAF


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

like image 24
Nam Le Avatar answered Nov 11 '22 03:11

Nam Le


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);
 }
});
like image 1
Daman Avatar answered Nov 11 '22 02:11

Daman