I've an issue with Cloud Code.My problem is explained as:
- I've a class "MyClass"
- I want only one object saved in my backend referring to a particular property ("myID")
- If a user try to save this object and there is not any with the same "myID" save it
- If a user try to save this object and THERE IS ALREADY ONE with the same "myID" update the field ("address") for the existing object.
I try to this using this code:
var MyObject = Parse.Object.extend("MyClass");
Parse.Cloud.beforeSave("MyClass", function(request, response) {
if (!request.object.get("myID")) {
response.error('A MyObject must have a unique myID.');
} else {
var query = new Parse.Query(MyClass);
query.equalTo("myID", request.object.get("myID"));
query.first({
success: function(object) {
if (object) {
object.set('address',request.object.get("address"));
object.save().then(function () {
response.error('Updated existing MyObject address');
},function (error) {
response.error("Error: " + error.code + " " + error.message);
});
} else {
response.success();
}
},
error: function(error) {
response.error("Could not validate uniqueness for this MyObject object.");
}
});
}
});
But this doesn't works and in my Parse.com Log says:
Result: Error: 142 Error: 124 Too many recursive calls into Cloud Code
I know can I achieve what I want?