3

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.)

3
  • Could you check that request.user is an valid Parse.User object and role did exist? If role did exist, it should work with role.getUsers().add(Parse.User.current()); Commented Nov 27, 2014 at 21:26
  • request.user is a valid user. Have tried your alternative as well and now in both cases I am getting the response No Message provided Commented Nov 27, 2014 at 21:46
  • For reference this helped me parse.com/questions/cloudcode-adding-user-to-role-on-beforesave I had multiple actions within one function, and could not get this to work. The issue was the response.success was being called someplace else in the function so my role set up wasnt getting time to complete. Wasted 3 hrs on it... damm javascript !! Commented Mar 30, 2015 at 10:11

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.