2015-06-21 72 views
3

我有一个模板,我试图显示所有用户在被调用的userList。显示流星的所有用户

提前帮助

//服务器

Meteor.publish("userList", function() { 

var user = Meteor.users.findOne({ 
    _id: this.userId 
}); 


if (Roles.userIsInRole(user, ["admin"])) { 
    return Meteor.users.find({}, { 
     fields: { 
      profile_name: 1, 
      emails: 1, 
      roles: 1 
     } 
    }); 
} 

this.stop(); 
return; 
}); 

谢谢!

+0

你可以通过'''this.userId' ''而不是'''user''例如'''if(Roles.userIsInRole(this.userId,['admin']) ){..}''' –

回答

5

这应该工作!

//服务器

Meteor.publish("userList", function() { 
      return Meteor.users.find({}, {fields: {emails: 1, profile: 1}}); 
    }); 

//在客户端

Meteor.subscribe("userList"); 
+0

感谢您的输入,但我无法在模板中迭代这些内容。你知道这是怎么回事吗? – Matt

10
,如果你想展示你可以在你publish.js文件尝试所有用户

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

在你的路由器中,你怀疑这个

Router.route('/users', { 
    name: 'usersTemplate', 
    waitOn: function() { 
     return Meteor.subscribe('userList'); 
    }, 
    data: function() { 
     return Meteor.users.find({});  
    } 
}); 

下一步是迭代模板中的数据。

如果您不想在路由器中订阅,您可以在模板级订阅,请阅读本文以获取更多详细信息。

https://www.discovermeteor.com/blog/template-level-subscriptions/

问候。

+0

你如何迭代模板中的数据? –

0

这应该工作。

  1. 订阅(客户端)
  2. 发布(服务器)

客户:

UserListCtrl = RouterController.extend({ 
    template: 'UserList', 
    subscriptions: function() { 
     return Meteor.subscribe('users.list', { summary: true }); 
    }, 
    data: function() { 
     return Meteor.users.find({}); 
    } 
}); 

服务器:

Meteor.publish('users.list', function (options) { 
    check(arguments, Match.Any); 
    var criteria = {}, projection= {}; 
    if(options.summary){ 
     _.extend(projection, {fields: {emails: 1, profile: 1}}); 
    } 
    return Meteor.users.find(criteria, projection); 
});