Situation:
I have a database at Parse.com. In this database I have objects which have an Array and a boolean.
Wanted Result:
I want the boolean to be set to true if the Array length is bigger than 0. And false if the size is 0.
Problem:
I do not want to set this boolean on adding/deleting from array, but i want some kind of listener that reacts on a change to the object.
A must
This solution has to be possible with only cloud code.
Is there any way to do this?
Absolutely and this can be easily accomplished using a cloud code beforeSave hook.
In the beforeSave of the desired class, look at the size of the array and set the bool accordingly. This will guarantee the bool is up-to-date for every save.
Here's some sample code
// Update the boolean based on the array length
Parse.Cloud.beforeSave("yourClass", function(request, response) {
var yourArray = request.object.get("yourArray");
if (yourArray.length > 0) {
request.object.set("booleanProperty", true);
} else {
request.object.set("booleanProperty", false);
}
response.success();
});