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

Manuel
  • 14,274
  • 6
  • 57
  • 130
  • 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());` – eth3lbert Nov 27 '14 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_ – Constantijn Schepens Nov 27 '14 at 21:46
  • For reference this helped me https://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 !! – DogCoffee Mar 30 '15 at 10:11

1 Answers1

4

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