Parse cloud code request.object.get(key) return always undefined
Asked Answered
D

1

0

i'm trying to make a simple check before saving an object on Parse. I'm using the "beforeSave" method to check if object has necessary fields setup correctly before saving.

The problem is that when i try to get a field of my object it return always undefined even if it is setup correcly in 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) {
    // return always an error with uuid printed
    response.error("Object uuid: " + request.object.get("uuid"));
}

This is a sample code, not my real code but as you can see the object passed to this function has "uuid" field inside but request.object.get("uuid") return always "undefined":

Console log:

Result: Object uuid: undefined

Official documentation say to access object fields in this way, so is this a Parse bug or i'm doing some mistakes?

EDIT 1:

As suggested i tryied to log the object in this way:

console.log("Object: " + JSON.stringify(request.object));
console.log("Request: " + JSON.stringify(request));

The result is:

Object: {}
Request: {"object":{}, <some other fields..>}

Any idea?

EDIT 2:

I reproduced correcly the error and it seems to be a bug with ACL; First i created a new object extending ParseObject (i'm working 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 it will create and save a test object in 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 uploaded this cloud code that does nothing else than 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 running it works and it saves correctly the object in the new table. Logs:

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 tryid to do was to set ACL to this object editing android code in this way:

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 running everything this is the log:

Input: {"original":null,"update":{"ACL":{"userAcl":{"read":true,"write":true}},"age":45,"name":"Tom"}}
Result: Update changed to {}
Object: {}

So is this enough?

EDIT 3:

The only solution i found (this is more a workaround) is to save the object without ACL from Android code and then set ACL in beforeSave on 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));
}
Damnatory answered 11/9, 2015 at 19:28 Comment(8)
why don't you log JSON.stringify(request.object)Othello
I tryied and it seems to be an empty object {}. As second test i tryied: console.log(JSON.stringify(request)); and it contain the empty object. So i think this is a parse bug.Damnatory
Before we conclude bug, how about proving that your code can save an object. Start with an empty table, create and save an object, prove using the data browser that all the data is there. Then add afterSave that only logs. If you can show that a new object appears in the data browser but that request.object is undefined in the afterSave, then maybe you have a bug. But consider the odds - this is a mainstream function in a mature service, and you're probably just coming up to speed. For now, I'd bet against parse.com bug.Othello
I have the same error as you on the Parse.beforeSave method so the error is not only limited to you. When i try to call request.object.get("column") I get an error I2015-09-12T06:57:57.285Z]Data : undefined. However, in my android log I could see the data being passed correctly to the cloud code. To add on, when I try to edit my data directly from the parse web app itself, I could see my cloud log data being passed correctly I2015-09-12T07:07:30.580Z] Data: Laptop. So I guess there may be a bug when called from the android application. Will update you if I found a solution.Causey
Yes, i noticed this bug only when ACL is setted for the object to save and beforeSave method is triggered. If ACL is not setted or if i remove beforeSave method from parseCloud it works perfectly. So i think this is a bug. If you discover some workaround please let me know! ThanksDamnatory
Try using Parse.Cloud.useMasterKey(); before you access any of the value.Causey
This not work, it continue to be an empty object if ACL is setted. The only solution i found so far is to save an object without ACL from android and set ACL in beforeSave on cloud code.Damnatory
Also experiencing this bug over here. Unfortunately removing the ACL does not do the trick for me.Acerose
H
1

I had a very similar problem. I was making changes to an existing project and when I uploaded the code I started getting error where no errors had been before.

I finally solved it by going back to jssdk version to 1.4.0 in .parse.project

Maybe it solves your problem also.

Highlands answered 8/10, 2015 at 14:7 Comment(1)
I think there is something to this but why would they want to make impossible for people to use Node if they have a Node library? Couldn't they just communicate it clearly if this is the case?Isoline

© 2022 - 2024 — McMap. All rights reserved.