Search code examples
javascriptparse-platformparse-cloud-code

Parse Cloud Getting all Users with User Role


we have an admin interface so CS can view all users with their role and also they have the option to change user's role for any particular user's We are using Parse cloud So, Can anyone tell me how can I fetch all user's with their role, I am fetching users by using this code but unable t understand how to get role belongs to every users

Parse.Cloud.define("UserWithRole", function(request, response) {
    const Userquery = new Parse.Query("User");
    Userquery.find({
      useMasterKey: true,
    }).then(function(data){
         /// Getting User List here but I want their roles also
    });

});

Environment "node-schedule": "^1.3.0", "parse": "^1.8.5", "parse-dashboard": "^1.0.19", "parse-server": "2.8.4",


Solution

  • You can try something like this:

    Parse.Cloud.define('UserWithRole', async request => {
      const usersQuery = new Parse.Query(Parse.User);
      const users = await usersQuery.find({ useMasterKey: true });
      return await Promise.all(users.map(async user => {
        const rolesQuery = new Parse.Query(Parse.Role);
        rolesQuery.equalTo('users', user);
        const roles = await rolesQuery.find({ useMasterKey: true });
        return {
          name: user.get('name'),
          roles,
          email: user.get('email');
        };
      }));
    });