Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Meteor how can I publish one server side mongo collection under different names?

Tags:

meteor

I have a server side mongo collection called Profiles.

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...

CLIENT SIDE

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
        } 
    });
});

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()});
});

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.

like image 864
Steeve Cannon Avatar asked Nov 03 '22 19:11

Steeve Cannon


1 Answers

I think your issue is more on the MongoDB side than with meteor. Given your case I'd do two collections (Group and Profile).

Each document in the Group collection would feature an array containing DBRefs to documents in the Profile collection (actually users so I would think about renaming the Profile collection to User as imo that's more intuitive).

Same for the Profile collection and its documents; each document in the profile collection (representing a user) would have an array field containing DBrefs to groups the user belongs to (documents inside the Group collection).

like image 167
Tom Avatar answered Nov 11 '22 09:11

Tom