Search code examples
javascriptparse-platformuser-management

Parse.com : Fetch all users with no role assigned


I have some users in which some of the users have administrator role assigned to them. All other users doesn't have a role assigned to them.

I am able to fetch only users who have administrator role assigned to them using the following code.

var query = new Parse.Query(Parse.Role);
query.equalTo("name", "administrator");
query.first({
    success : function(role) {
        var relation = role.getUsers();
        relation.query().find({
            success : function(results) {
                console.log(results);
            },
            error : function(error) {
                console.log(error);
            }
        });
    },
    error : function(error) {
        console.log(error);
    }
});

How can I fetch users to whom any role is not assigned?


Solution

  • Try Parse.Query.notEqualTo:

    var query = new Parse.Query(Parse.Role);
    query.notEqualTo("name", "administrator");
    query.first({
        success : function(role) {
            var relation = role.getUsers();
            relation.query().find({
                success : function(results) {
                    console.log(results);
                },
                error : function(error) {
                    console.log(error);
                }
            });
        },
        error : function(error) {
            console.log(error);
        }
    });
    

    Also, maybe these filters may help you

    query.doesNotExist("name");
    query.notContainedIn("role", ["Administrator"]);
    

    https://parse.com/docs/js/guide#queries-query-constraints