0

I want to add users to my Parse.Role but it's not working. I looked at a number of examples and they all seem pretty straight-forward however I'm unable to get it right. Here is my code:

Parse.Cloud.define("activateVendor", function(request, response){
     var query = new Parse.Query(Parse.Role);
     query.equalTo("name", "vendor");
     query.first ({
          success: function(role) {
               role.getUsers().add(request.params);//request.params is the parse object, should I be using request.params.id?
               role.save();
          },
          error: function(error) {
               throw "Got an error " + error.code + " : " + error.message;
          }
      })
});

2 Answers 2

1

It depends on who you wish added to the role. The user who made the cloud request is available via the request object...

role.getUsers().add(request.user);

Otherwise, you can get a user via a query as @RobertRowntree suggests. (though, I'd suggest doing it with promises).

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

Comments

1

does what you want with 2 queries (Role, User) ...

  var qu = new Parse.Query(Parse.User);
  var qr = new Parse.Query(Parse.Role);
  Parse.Cloud.useMasterKey();
  qr.get(roleId, {
  success: function(role) {
      _role = role;
      qu.get(userId, {
        success: function(user) {
            _role.getACL().setRoleReadAccess(_role, true);
            _role.getUsers().add(user);
            _role.save();
            response.success(_role.toJSON());
        },
        error: function(object, error) {

        }
      });
  },
  error: function(object, error) {

  }
});

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.