Subscribe to the Meteor.Users Collection

// in server.js Meteor.publish("directory", function () { return Meteor.users.find({}, {fields: {emails: 1, profile: 1}}); }); // in client.js Meteor.subscribe("directory"); 

Now I want to get the directory lists requested from the client, for example directory.findOne() , from the browser console. // Testing

Doing directory=Meteor.subscribe('directory') / directory=Meteor.Collection('directory') and doing directory.findOne() doesn't work, but when I do directory=new Meteor.Collection('directory') it works and returns undefined, and I'm sure it CREATES a collection of mongos on the server, which I don’t do because the USER collection already exists and points to a new collection, not a USER collection.

NOTE. I don’t want to get involved with the way the Meteor.users collection processes its function ... I just want to get some specific data from it using another descriptor that will return only the specified fields and not override its default function ...

Example:

 Meteor.users.findOne() // will return the currentLoggedIn users data directory.findOne() // will return different fields taken from Meteor.users collection. 
+8
mongodb meteor
source share
2 answers

If you want this setting to work, you need to do the following:

 Meteor.publish('thisNameDoesNotMatter', function () { var self = this; var handle = Meteor.users.find({}, { fields: {emails: 1, profile: 1} }).observeChanges({ added: function (id, fields) { self.added('thisNameMatters', id, fields); }, changed: function (id, fields) { self.changed('thisNameMatters', id, fields); }, removed: function (id) { self.removed('thisNameMatters', id); } }); self.ready(); self.onStop(function () { handle.stop(); }); }); 

No on the client side you need to define a collection on the client side:

 directories = new Meteor.Collection('thisNameMatters'); 

and subscribe to the appropriate data set:

 Meteor.subscribe('thisNameDoesNotMatter'); 

This should work now. Let me know if you think this explanation is not clear enough.

EDIT

Here, the self.added/changed/removed methods act more or less like an event dispatcher. In short, they give instructions to every customer who called

 Meteor.subscribe('thisNameDoesNotMatter'); 

about updates that should be applied to a collection of clients named thisNameMatters , assuming that this collection exists. The name passed as the first parameter can be chosen almost arbitrarily, but if there is no corresponding collection on the client side, all updates will be ignored. Please note that this collection can only be client-side, therefore it does not have to correspond to the "real" collection in your database.

Returning the cursor from the publish method, this is just a shortcut to the above code, with the only difference being that theNameMatters name of the actual collection is used theNameMatters the actual collection. This mechanism actually allows you to create as many “mirrors” of your data sets as you like. In some situations, this can be very useful. The only problem is that these “collections” will be read-only (which is completely pointless to BTW), because if they are not defined on the server, the corresponding `insert / update / remove 'methods do not exist.

+12
source share

The collection is called Meteor.users , and there is no need to declare a new one either on the server or on the client.

Your publish / subscribe code is correct:

 // in server.js Meteor.publish("directory", function () { return Meteor.users.find({}, {fields: {emails: 1, profile: 1}}); }); // in client.js Meteor.subscribe("directory"); 

To access the documents in the user collection that were published by the server, you need to do something like this:

 var usersArray = Meteor.users.find().fetch(); 

or

 var oneUser = Meteor.users.findOne(); 
+5
source share

All Articles