Parse.com CloudCode add user to existing role not working

Here is the cloud-based JavaScript I am trying to use. When a new _User is created, I want to add them to my Client role.

Parse.Cloud.afterSave(Parse.User, function(request) {
    Parse.Cloud.useMasterKey();

    query = new Parse.Query(Parse.Role);
    query.equalTo("name", "Client");
    query.first ({
        success: function(role) {
            role.getUsers().add(request.user);
            role.save();
        },
        error: function(error) {
            throw "Got an error " + error.code + " : " + error.message;
        }
    });
});

      

This takes code directly from the Parse.com role example. The code works successfully when the new _User is saved, returning Result: Success

, but when I check the "users" associated with that role in the data browser, nothing happened.

I also tried to substitute role.getUsers().add(request.user);

for role.relation("users").add(request.user);

according to the example on the old Parse.com forum, but no difference. It seems like it should be really straight forward, so I'm not sure what I am doing wrong.

(I manually used the REST API using curl to add _Users to the client role and it works, so I know it should work.)

+3


source to share


1 answer


It turns out you need to use request.object

instead request.user

. Now it works!



+4


source







All Articles