使用meteor列出客户端上的所有用户

dan*_*ane 8 coffeescript meteor

根据meteor文档,如果安装了autopublish包,则应将所有用户发布到所有客户端.

http://docs.meteor.com/#meteor_users

我安装了autopublish包,但仅使用forEachon Meteor.users列出当前登录的用户.

有没有更正确的方法来使用coffeescript列出客户端上的所有用户?

ami*_*sim 9

以下是Meteor 派对示例的摘录:

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

// in client.js
Meteor.subscribe("directory");
Run Code Online (Sandbox Code Playgroud)


cra*_*ngs 8

如果您在不使用订阅的情况下自动发布用户集合

if Meteor.isServer

    Meteor.publish null, ->
        Meteor.users.find {},
            fields:
                username: 1
                profile: 1
Run Code Online (Sandbox Code Playgroud)

如果您想订阅,请指定用户

if Meteor.isServer

    Meteor.publish 'users-by-selector', (options) ->
        Meteor.users.find options, # options as selector like $in: name: 'john'
            fields: # use fields to only publish specify fields you want to send. 
                username: 1
                profile: 1

if Meteor.isClient

    Meteor.autosubscribe ->
        options = Session.get 'your mongodb selector'
        Meteor.subscribe 'users-by-selector', options, ->
            console.log 'on Subscribe Complete Callback.'
            if Meteor.users.find().count()
                Session.set 'specifyUsersLoaded', true
Run Code Online (Sandbox Code Playgroud)

  • 伟大而有用的答案+1,但假设CoffeeScript为-1.问题未标记为CoffeeScript.它不叫Meteor.coffee (29认同)
  • 把它当作coffescript (4认同)
  • @danielsvane你的问题在哪里使用CoffeeScript? (2认同)