Meteor: Publishing all users to the client

前端 未结 1 411
南旧
南旧 2021-02-04 12:26

Why would this not work?:

On both the client and server:

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

On the server only:

         


        
相关标签:
1条回答
  • 2021-02-04 13:06

    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.
    0 讨论(0)
提交回复
热议问题