如何根据当前登录用户从Meteor.users集合中返回SPECIFIC用户?

时间:2016-12-06 06:58:59

标签: mongodb meteor meteor-accounts

我担心Meteor账户的灵活性。我想根据当前登录用户中的特定属性(Meteor.users内)将Meteor.user.profile集合返回给客户端

例如,具有HR职位的用户只能返回在他下工作的用户。在他下工作的员工将拥有employeeHR属性。

现在我想要你们知道我计划采取的方法是否可行?

是否有更好的方法来实现我的目的?

更新

我使用以下代码来测试我是否可以检索authorityLevel的值,并且它已成功打印到控制台:

Session.set('checkAuthority', Meteor.user().profile.authorityLevel);
console.log(Session.get('checkAuthority'));

然后,我尝试根据用户authorityLevel返回用户,这是代码:

if(!Meteor.user().profile.authorityLevel){
            return Meteor.users.find({}, { sort: {createdAt: -1}});
        }
        if(Meteor.user().profile.authorityLevel === "HR") {
            return Meteor.users.find({authorityLevel: "HRemployee"}, { sort: {createdAt: -1}});
        }

它不起作用,我在控制台中收到以下错误

Exception in template helper: TypeError: Cannot read property 'profile' of null
    at Object.employees

我在这里做错了什么?

注意:

即使使用具有authorityLevel属性

的用户登录,我也会收到相同的错误

2 个答案:

答案 0 :(得分:0)

Meteor账户具有很大的灵活性。

一种方法可能是使用alanning:roles包,它允许您定义角色,也可以使用组。

您可以定义组以反映组织(例如部门)。如果HR用户具有管理角色,则可以查看该组中的用户。

答案 1 :(得分:0)

在将Meteor.user()完全发布到客户端之前,您正在查看它。做:

if( Meteor.user() && !Meteor.user().profile.authorityLevel )

在尝试阅读profile密钥之前确保它全部存在。

如果有任何安慰,每个人都会在Meteor学习曲线的某个时刻遇到这个问题。