I am trying to do a simple check before storing an object in Parse. I use the "beforeSave" method to verify that the objects are set correctly before saving.
The problem is that when I try to get the field of my object, it always returns undefined, even if it is correctly configured in the log!
Input: {"original":null,"update":{"ACL":{"abcdefghi":{"read":true,"write":true}},"isDeleted":false,"lastEdit":0,"name":"testobject","uuid":"109d0b30-1ad5-408b-ba49-2ce024935476"}}
and cloud code:
Parse.Cloud.beforeSave("MyObject", function (request, response) {
This is sample code, not my real code, but since you can see that the object passed to this function has a "uuid" field inside, and request.object.get ("uuid") always returns "undefined":
Console Log:
Result: Object uuid: undefined
The official documentation says that accessing the object fields is thus a Parse error, or am I making some errors?
EDIT 1:
As I suggested, I tried to register the object this way:
console.log("Object: " + JSON.stringify(request.object)); console.log("Request: " + JSON.stringify(request));
Result:
Object: {} Request: {"object":{}, <some other fields..>}
Any idea?
EDIT 2:
I reproduced the error, and it looks like it is an ACL error; First, I created a new object extending ParseObject (I work on Android):
@ParseClassName("Person") public class ParsePerson extends ParseObject { public void setName(String name) { put("name", name); } public void setAge(int age) { put("age", age); } public String getName() { return getString("name"); } public int getAge() { return getInt("age"); } }
And than my background thread will create and save the test object this way:
ParsePerson parsePerson = new ParsePerson(); parsePerson.setName("Tom"); parsePerson.setAge(45); try { parsePerson.save(); } catch (ParseException e) { Debug.e("Error code: "+ e.getCode(), e); }
Than I downloaded this cloud code, which does nothing more than a log:
Parse.Cloud.beforeSave("Person", function (request, response) { // log person object console.log("Object: " + JSON.stringify(request.object)); // respond always success to save the object response.success(); }); Parse.Cloud.afterSave("Person", function(request) { // log person object again console.log("Object: " + JSON.stringify(request.object)); // respond success response.success(); });
After starting, it works, and it correctly saves the object in a new table. Magazines:
Input: {"original":null,"update":{"age":45,"name":"Tom"}} Result: Update changed to {"age":45,"name":"Tom"} Object: {"age":45,"name":"Tom"}
The last thing I tried to do was set the ACL for this Android code editing object like this:
ParsePerson parsePerson = new ParsePerson(); parsePerson.setName("Tom"); parsePerson.setAge(45); parsePerson.setACL(new ParseACL(ParseUser.getCurrentUser())); // new line try { parsePerson.save(); } catch (ParseException e) { Debug.e("Error code: "+ e.getCode(), e); }
And after starting all this magazine:
Input: {"original":null,"update":{"ACL":{"userAcl":{"read":true,"write":true}},"age":45,"name":"Tom"}} Result: Update changed to {} Object: {}
So is that enough?
EDIT 3:
The only solution I found (this is more of a workaround) is to save the object without an ACL from the Android code, and then set the ACL in beforeSave in the cloud code:
Parse.Cloud.beforeSave("Person", function (request, response) { // setup ACL for this object var user = request.user; var newACL = new Parse.ACL(); newACL.setPublicReadAccess(false); newACL.setPublicWriteAccess(false); newACL.setReadAccess(user.id, true); newACL.setWriteAccess(user.id, true); request.object.setACL(newACL); // log person object again console.log("Object: " + JSON.stringify(request.object)); }