我正在创建和应用以允许人们进行协作,因此我创建了groups
人们可以分配给他们。在服务器上,为了最大限度地减少客户端开销和安全性,我只需要publishing
与其组相关的数据。我的代码:
Meteor.publish('lists', function() {
var user = Meteor.users.findOne(this.userId);
return Lists.find({group: user.profile.group});
});
我获取用户对象并根据该用户group
过滤发布的内容。实际上,group
中应该有一个profile
ID。但在测试期间,我创建了没有group
的用户。而且我还创建了lists
,但却没有group
。
当我在我的测试用例中调试.log user.profile.group
时,我看到undefined
正如预期的那样。问题是,带有未定义选择器值的.find()
查询以某种方式返回 不 拥有group
的所有文档。就像Mongo告诉我" 嗯,你没有给我们一个定义的组,所以这里列出了没有组参数的所有列表一点都没有!"
我做错了什么?我认为.find({selector-key: selector-value})
只有在找到与选择键/值匹配的数据时才会返回游标?
答案 0 :(得分:1)
使用以下方法防范退化情况:
Meteor.publish('lists', function() {
var user = Meteor.users.findOne(this.userId);
if ( user && user.profile && user.profile.group ) return Lists.find({group: user.profile.group});
this.ready();
});