简体   繁体   中英

Parse.com - cannot add specific user to role

I cant seem to query a specific user and add him to a role on Parse.com using the JS API. What did I do wrong?

var query = new Parse.Query(Parse.User);
query.equalTo("username", "testParent");  
query.find({
success: function(person) {
// Do stuff

var roleACL = new Parse.ACL();
var role = new Parse.Role("Parent", roleACL);
role.getUsers().add(person[0]);
role.save();


}
});

Here's how to add any user to a role, given a role name:

function addUserToRoleNamed(user, roleName) {
    var query = new Parse.Query(Parse.Role);
    query.equalTo("name", roleName);
    return query.first().then(function(role) {
        var relation = role.relation("users");
        relation.add(user);
        return role.save();
    });
}

Here's how to get a user and call that function:

var query = new Parse.Query(Parse.User);
query.equalTo("username", "testParent");  
return query.first().then(function(user) {
    return addUserToRoleNamed(user, "Parent");
}).then(function(result) {
    console.log("success: " + JSON.stringify(result));
}, function(error) {
    console.log("error: " + JSON.stringify(error));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM