2015-06-21 76 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); 
});