基于当前用户属性发布流星集合
Publish Meteor Collection based on current user property
我正在尝试发布基于组一个用户所属的敏感信息的集合。
数据的例子:Items:
ItemA - GroupA
ItemB - GroupA
ItemC - GroupB
Users:
UserA - GroupB
UserB - GroupA
UserC - GroupA
但是当我试着做
Meteor.publish('groupItems', function () {
return Items.filter({ groupId : Meteor.user().profile.groupId });
}
失败,因为在这样的呼叫中我只允许访问Meteor.userId()
。(在这个模糊的错误消息中表示,服务器端:
Exception from sub 5jnantRJ6gyHpTkTy Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.
问题:如何根据用户属性(如groupId
)过滤Collection
在publish内部,如果用户登录了,您就可以访问它。userId,它允许您查询数据库以获取其余的用户信息。所以你可以将publish重写为:
Meteor.publish('groupItems', function () {
if ( ! this.userId ) return []; //return an empty array if no user is logged in.
var user = Meteor.users.find( this.userId );
return Items.filter({ groupId : user.profile.groupId });
});
相关文章:
- 如何查找流星集合中最后一个元素/对象的id
- 上传与流星集合相关的图像
- 如何用图形填充流星集合并单独显示它们
- 如何使流星集合非反应性取决于网址
- 在流星集合文档中插入值
- 为什么将数据加载到流星集合中需要这么长时间
- 一个物体来自什么流星集合
- 流星集合不会在刷新时加载
- 将特定字段从流星集合文档中检索到JS文件中并去除HTML标签
- 专用文件中的流星集合
- 更新流星集合中的子数组列表
- 如何替换流星集合查询中的排序关键字字段
- 基于当前用户属性发布流星集合
- 流星集合findOne在回调中调用时返回undefined
- Angular流星集合onchange
- ui-router resolve中的流星集合
- 处理流星集合中可变的键值对(无模式困境)
- 流星集合在服务器上是空的
- 改变流星.集合
- 流星集合未在客户端更新订阅