Mo Adel
Mo Adel

Reputation: 1156

Parse cloud beforeSave update class

I have class named Profiles on Parse, which contains a Boolean column isDefault.

Basically what I am trying to achieve is, if a save is called on Profiles, I want to check the object added/updated before it's saved, if it's isDefault is true, then change all other rows in Profiles to false, then save the new or updated object.

For this scenario, I used beforeSave, Everything work in beforeSave except that it doesn't run the query which updates all Profiles data, here is the code I use:

Parse.Cloud.beforeSave("Profiles", function(request, response) {


    if (request.object.get("imageUrl") == null || request.object.get("imageUrl") == "") {
    request.object.set("imageUrl", "http://www.imran.com/xyper_images/icon-user-default.png");
    }

  if (!request.object.get("isDefault")) {
    request.object.set("isDefault", false);
  }else{
    var query = new Parse.Query("Profiles");
      query.equalTo("user", request.object.get("user"));
      query.find().then(function(results) {
            for (var i = 0; i < results.length; i++) { 
              var pro = results[i];
              pro.set("isDefault", false);
              pro.save();
            }
        }
      ); 
  }
  response.success();
});

Upvotes: 0

Views: 1019

Answers (1)

ardrian
ardrian

Reputation: 1249

Note that in the Javascript SDK, save() is an async method which returns a promise (https://parse.com/docs/js/symbols/Parse.Object.html#save).

In your code, response.success() is probably returning before the save() operation has been completed on all objects.

Parallel Promises

I would solve this by using parallel promises (You could also try an approach using serial promises, or using the saveAll method):

var query = new Parse.Query("Profiles");
query.equalTo("user", request.object.get("user"));
query.find().then(function(results) {

    var promises = [];

    for (var i = 0; i < results.length; i++) {
        var pro = results[i];
        pro.set("isDefault", false);
        promises.push(pro.save());
    }

    return Parse.Promise.when(promises);
}).then(function(result) {
    response.success();
},function(error){
    response.error(); 
});

SaveAll

Here is a solution using the saveAll method. According to the Parse JS docs, saveAll() doesn't return a promise. So the syntax here gets a little messy (I don't like mixing promises as I've done here, but I didn't want to modify your original code too much).

var query = new Parse.Query("Profiles");
query.equalTo("user", request.object.get("user"));
query.find().then(function(results) {

    for (var i = 0; i < results.length; i++) {
        var pro = results[i];
        pro.set("isDefault", false);
    }

    Parse.Object.saveAll(results, {
        success: function(list) {
            response.success();
        },
        error: function(error) {
            response.error();
        }
    });

}, function(error){
    response.error();
});

Upvotes: 1

Related Questions