如何根据当前登录用户从Meteor.users集合中返回特定用户?
问题描述:
我很关心流星账户的灵活性。我想根据当前登录用户的特定属性(内部Meteor.user.profile
)将Meteor.users
集合返回给客户端。例如,具有HR位置的用户只能返回正在其下工作的用户。而在其下工作的员工将拥有employeeHR
属性。如何根据当前登录用户从Meteor.users集合中返回特定用户?
现在我想从你们那里知道我打算采取的方法是否可行?
有没有更好的方法来达到我的目的?
UPDATE:
我用下面的代码来测试,如果我可以检索的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}});
}
它不起作用,我得到foll在控制台中的错误
Exception in template helper: TypeError: Cannot read property 'profile' of null
at Object.employees
我在做什么错在这里?
注:
我得到同样的错误,即使具有authorityLevel
属性
答
流星账户有足够的灵活性,用户登录时。
一种方法可以是使用alanning:roles包,它将允许您定义角色和组。
您可以定义组以反映组织(例如部门)。 HR用户(如果他们具有管理角色)可以看到该组中的用户。
答
您正在查看Meteor.user()
,然后才能完全发布给客户。这样做:
if(Meteor.user() && !Meteor.user().profile.authorityLevel)
,以确保这一切都没有尝试读取profile
键之前。
如果有任何安慰,每个人都会在他们的Meteor学习曲线的某个点上遇到这个问题。
我想实现我认为是更快的解决方案,而不是使用另一个软件包。我更新了我的问题,如果您可以看看,请 –