I am following the instruction here:
https://parse.com/questions/unique-fields--2
var Profile = Parse.Object.extend("Profile");
Parse.Cloud.beforeSave("Profile", function(request, response) {
if (!request.object.get("entityID")) {
response.error('must have a entity id');
} else {
var query = new Parse.Query(Profile);
query.equalTo("entityID", request.object.get("entityID"));
query.first({
success:function(object) {
if (object) {
response.error("already exist");
}
else {
response.success();
}
},
error:function(error) {
response.error("couldnt validate uniqueness for this profile");
}
});
}
});
I have a profile table and the unique key is entityID, when inserting, I need to check if the entry with the same key already exist.
The problem is, I am not able to update the entry anymore (it seems both insertion and update request trigger this cloud code.
How can i modify it so that
when i insert, if it exists, I ignore it; if not exist, insert it.
when i update, if it exist, update it; if not exist, i ignore it.