2

So its my first time writing Javascript so please bear with me. I wrote this function in order to query a class in my Parse.com application, and then after querying I want to set one of the columns (of type Boolean) to true.

I set up a test class with only 7 values in order to test.

The problem: only 3 out of 7 are being changed. Do I have to wait after each save? I know that waiting/sleeping in Javascript is "wrong" but I can't seem to find a solution.

Thanks in advance!

Additionally, when using iOS/Parse, I would like to check if the boolean value is undefined in Objective-C, I already tried to compare it to nil/NULL, an exception was thrown

Parse.Cloud.define("setYears", function(request, response) {
var object = new Parse.Query("testClass");

object.find({
success: function(results)
{
for (var i = 0; i < results.length; i++) {
    results[i].set("testBool",true);// = true;
    results[i].save(null,

         {
            success:function ()
            {
                response.success("Updated bool!");
            },
            error:function (error)
            {
                response.error("Failed to save bool. Error=" + error.message);
            }

        });

};

response.success();

}
})
});
Fattie
  • 27,874
  • 70
  • 431
  • 719
RJiryes
  • 951
  • 10
  • 25
  • it's incredibly difficult to solve this :) it's a real mess in parse. note that the first time you get to a "response" that will sort of finish everything. you may want to ask on the https://groups.google.com/forum/#!forum/parse-developers group... – Fattie Sep 01 '14 at 10:53
  • I had a feeling it wasn't that simple. Thanks for letting me know. – RJiryes Sep 01 '14 at 10:58
  • Maybe you can help with another thing, I would like to check if the boolean value is undefined in Objective-C, I already tried to compare it to nil/NULL, an exception was thrown. Do you know of any other way? :) – RJiryes Sep 01 '14 at 11:00

1 Answers1

3

It turned out to be not that difficult to solve as stated above. Just had to use saveAll instead of saving each object by itself. Here is the correct solution if anybody needs it:

Parse.Cloud.define("setYears", function(request, response) {
var object = new Parse.Query("testClass");

object.find({
success: function(results)
{
for (var i = 0; i < results.length; i++) {
    results[i].set("testBool",true);// = true;  
}

Parse.Object.saveAll(results,{
success: function(list) {
  // All the objects were saved.
  response.success("ok " );  //saveAll is now finished and we can properly exit with confidence :-)
},
error: function(error) {
  // An error occurred while saving one of the objects.
  response.error("failure on saving list ");
},
});


response.success();

}


  })

});
RJiryes
  • 951
  • 10
  • 25