Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor: Publishing all users to the client

Tags:

meteor

Why would this not work?:

On both the client and server:

AllUsers = new Meteor.Collection('allUsers');

On the server only:

Meteor.publish('allUsers', function() {
return Meteor.users.find();
});

On the client only:

Deps.autorun(function(){
Meteor.subscribe("allUsers");
});

After I start up this app, AllUsers.find().count() is 0 but doing db.users.find().count() in the terminal gives the correct number (3). Even after I add a new user in the browser (using the standard ui-accounts package form), which should certainly cause the users collection to change, I still have no documents in my AllUsers collection. I'm beating my head against the wall trying to solve this!

like image 996
Michael McC Avatar asked Dec 12 '13 22:12

Michael McC


1 Answers

Meteor collections defined on the server have a 1:1 relationship with collections in the database. You don't have a collection in the DB called 'allusers', so that definition doesn't make sense. It seems like you are confusing the notion of a database collection and a published result set.

When you add the accounts package to your project, meteor defines the Meteor.users collection for you on both the client and the server, so you don't need to do that again. Your code looks fine - just remove the new Meteor.Collection and access the users via Meteor.users.find.


Collection Definitions Explained

Collection created for you by a package

  • example: Meteor.users is created by the accounts package
  • notes: The collection in the DB is db.users. You can make this collection look like the others you define by doing something like Users = Meteor.users, and later calling Users.find() instead of Meteor.users.find().

Collections defined on both the client and the server

  • example: new Meteor.Collection('rooms')
  • notes: In most projects, this is what you do nearly all of the time. The string 'rooms' is the name of the collection in the database (db.rooms). Documents can be published from the server to the client.

Collections defined on the client but not on the server

  • example: new Meteor.Collection('userCount')
  • notes: The server can write to the client collection, but it is not used to sync data to the DB. The typical use case is to inform the client with size or other metadata about another collection. Here the string 'userCount' does not correspond to the name of a DB collection, but instead is simply an identifier agreed upon by the client and server.
  • see also: How To: Publish to a Client Only Collection and Meteor Subscribe and Display Users Count

Unmanaged local collection defined on the client

  • example: new Meteor.Collection(null)
  • notes: These are defined when the client wants to have collection semantics, but doesn't want to use the collection to communicate with the server. A typical use case would be a demo application, where the user can play with an interface but only alter the data inside her own browser.
like image 59
David Weldon Avatar answered Nov 15 '22 20:11

David Weldon