Skip to content Skip to sidebar Skip to footer

Parse Cloud Update Unique Column

I have used this code to save the data using Parse Cloud, I have a unique column 'number_plate' I am facing a problem and that is when I try to update the object I'm unable to do s

Solution 1:

The key in this case is to work with the promises.

What you are trying to reach is something like the following:

Parse.Cloud.beforeSave("Car", function(request, response) {
    if (!request.object.isNew()) {
      // Let existing object updates go through
      response.success();
    }
    var query = new Parse.Query("Car");
    // Add query filters to check for uniqueness
    query.equalTo("number_plate", request.object.get("number_plate"));
    query.first().then(function(existingObject) {
      if (existingObject) {
        // Update existing object. here you can do all the object updates you want 
        if (request.object.get("columnToUpdate") != undefined){
          existingObject.set('columnToUpdate',request.object.get("columnToUpdate"));
        }
        return existingObject.save();
        response.error();
      } else {
        // Pass a flag that this is not an existing object
        return Parse.Promise.as(false);
      }
    }).then(function(existingObject) {
      if (existingObject) {
        // Existing object, stop initial save
        response.error("Existing object");
      } else {
        // New object, let the save go through
        response.success();
      }
    }, function (error) {
      response.error(error);
    });
});

Post a Comment for "Parse Cloud Update Unique Column"