Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 411
Constantijn Schepens Avatar asked Apr 15 '26 06:04

Constantijn Schepens


1 Answers

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

like image 74
Constantijn Schepens Avatar answered Apr 17 '26 20:04

Constantijn Schepens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!