Search code examples
javascriptparse-platformparse-cloud-codeparse-javascript-sdk

Parse.com CloudCode add user to existing role not working


Here is the Parse javascript cloud code I am trying to use. As 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 is taking code directly from Parse.com's Role example. The code runs happily when a new _User is saved, returning Result: Success, but when I check the "users" tied to that Role in the Data Browser, nothing has happened.

I have also tried substituting role.getUsers().add(request.user); for role.relation("users").add(request.user); as per an example on Parse.com's old forum, but no difference. This seems like it should be really straight forward, so I'm not sure what I'm doing wrong.

(I have manually used the REST API, using curl, to add _Users to the Client Role, and this does work, so I know it should work.)


Solution

  • Turns out you need to use request.object instead of request.user. Now it works!