本文介绍了在 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.

这样管理员就可以编辑、更新等...每个配置文件集合项.

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.

推荐答案

我不完全确定您是如何检查错误的,但我认为您可能遇到了我遇到的问题.当您使用 Profiles 集合发布数据时,即使 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 集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 02:10
查看更多