在Meteor中,如何以不同的名称发布一个服务器端mongo集合? [英] In Meteor how can I publish one server side mongo collection under different names?

查看:60
本文介绍了在Meteor中,如何以不同的名称发布一个服务器端mongo集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Profiles的服务器端mongo集合.

I have a server side mongo collection called Profiles.

如果用户为adminId,我需要发布和订阅个人档案的整个集合.

I need to publish and subscribe to the entire collection of Profiles if user: adminId.

这样,管理员可以编辑,更新等...每个Profile集合项.

That way the administrator can edit, updated, etc... each Profile collection item.

但是我希望用户能够看到他们的个人资料记录.

But I want users to be able to see their Profile record.

所以我尝试了这个...

So I tried this...

客户侧

MyProfile = new Meteor.Collection("myprofile");
Meteor.subscribe('profiles');
Meteor.subscribe('myprofile');

常用-客户端和服务器端

COMMON - CLIENT AND SERVER SIDE

Profiles = new Meteor.Collection("profiles");

服务器端-概要文件的发布和订阅工作正常.

SERVER SIDE - The publishing and subscribing of profiles works fine.

// this returns all profiles for this User
// if they belong to an ACL Group that has acl_group_fetch rights
Meteor.publish("profiles", function() { 
    var user_groups = Groups.find({users: this.userId()});
    var user_groups_selector = [];
    user_groups.forEach(function (group) {
       user_groups_selector.push(group._id);
    });
    return Profiles.find( {
       acl_group_fetch: {
          $in: user_groups_selector
        } 
    });
});

这似乎是问题开始的地方. Profiles.find返回集合项,因为我可以将它们输出到控制台服务器端.但是由于某种原因,发布和订阅无法正常工作.客户端什么也收不到.

Here is where the problem seems to begin. The Profiles.find is returning collection items because I can output them to the console server side. But for some reason the publish and subscribe is not working. The client receives nothing.

//  return just the users profile as myprofile
Meteor.publish("myprofile", function() {
  return  Profiles.find({user: this.userId()});
});

任何想法我在做什么错.我希望能够发布用户A可以插入,获取,更新,删除的记录的集合,但是用户B(C,D和E)只能看到他们的记录.

Any ideas what I am doing wrong. I want to be able to publish collections of records that User A can insert, fetch, update, delete but User B (C, D and E) can only see their record.

推荐答案

我不确定您是否正在检查错误,但我认为您可能遇到了我遇到的问题.当您使用个人档案"集合发布数据时,即使pub/sub调用使用"myprofile"名称,数据也始终在您要为其返回游标的集合中可用...在这种情况下,您所使用的数据在"myprofile"出版物中发布的内容将显示在客户端的"profiles"集合中.发布调用不会在客户端上创建"myprofile"集合.因此,如果您尝试在"myprofile"集合上查找(),则不会看到任何数据. (Meteor/MongoDB不会抱怨该集合不存在,因为当您引用它时,它们总是会懒惰地创建它.)

I'm not entirely sure of how you're checking for the error or not, but I think you may be running into a gotcha I ran into. When you publish data with the Profiles collection, even though the pub/sub calls use the 'myprofile' name, the data always is always available in the collection that you're returning a cursor for... in this case, the data you publish in the 'myprofile' publication will show up in the 'profiles' collection on the client. The publish call doesn't create a 'myprofile' collection on the client. So if you're trying to find() on the 'myprofile' collection you won't see any data. (Meteor/MongoDB won't complain that the collection doesn't exist because they always will lazily create it when you reference it.)

这篇关于在Meteor中,如何以不同的名称发布一个服务器端mongo集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆