2017-01-31 46 views
0

我需要從該角色獲取所有用戶。我已經知道這個代碼,但我不知道如何讓角色的用戶。如何獲取acl角色對應的所有用戶?

我只知道我需要使用getUsers()功能的作用,但我卡住。

我的代碼:

self.getCompanyUsers = function getCompanyUsers() { 
    $rootScope.displayLoading = true; 
    var userQuery = new Parse.Query(Parse.Role); 
    userQuery.contains('name', $state.params.id); 

    userQuery.find().then(function(roles) { 
     $scope.users = roles; 
     $rootScope.displayLoading = false;  
    }).then(function() { 
     console.log(roles.getUsers()); 

    }) 
}; 

回答

1

似乎有與您所提供的代碼,這將需要之前固定的幾個問題。

首先;你的承諾流程似乎有點不足。您正在嘗試繼續操作而不返回任何內容。我不確定爲什麼你需要在承諾鏈中這樣做,因爲你不必等待任何事情的完成。所以你應該刪除第二個then,除非你從發佈的代碼中省略了某些東西。

其次;您正試圖在Parse.Role的數組上調用getUsers()。有幾個方式這一輪,這取決於最終使用這個功能:

  • 您可以更改userQuery.find()userQuery.first(),這樣只會返回一個對象,並roles.getUsers()將是有效的。
  • 或者您可以遍歷從現有查詢中獲得的結果並在每個結果上調用getUsers()。我建議這不是你想要做的,因爲它可能會導致對用戶對象的很多查詢,如果這是你想要的,可能會有更好的選擇。

第三; getUsers()將簡單地返回Parse.Relation而不是角色中的用戶。要獲取用戶,您必須先獲取查詢對象。像這樣:role.getUsers().query();

這是一個正常的Parse.Query,你可以使用它作爲這樣。例如,role.getUsers().query().find()

如果我是正確的思維,你會想一些與此類似:

self.getCompanyUsers = function getCompanyUsers() { 
     $rootScope.displayLoading = true; 
     var userQuery = new Parse.Query(Parse.Role); 
     userQuery.contains('name', $state.params.id); 
     userQuery.first().then(function(role) { 
      $scope.users = role; 


      if(!role) 
      { 
       //check a role has been found 
       return Parse.Promise.error("No role found") 
      } 

      //role.getUsers() will be the Parse.Relation 
      //role.getUsers().query() will be a normal Parse.Query 
      return role.getUsers().query().find(); 

     }).then(function(users) 
      { 
       //users will be an array of the users in the role, depending on ACL/CLP. 
       console.log(users); 
       $rootScope.displayLoading = false; 
      } 
     ) 
    }; 
相關問題