2017-06-18 118 views
1

我想以表格形式顯示所有用戶信息,作爲管理頁面的一部分。我用流星賬戶的ui軟件包也是一樣。在流星帳戶中迭代用戶帳戶UI

的HTML代碼是:

{{#each userList}} 
<tbody> 
    <tr> 
    <th scope="row">*</th> 
    <td>{{infofullname}}</td> 
    <td>{{infosurname}}</td> 
    <td>{{infoemail}}</td> 
    </tr> 

</tbody> 
{{/each}} 

的問題是越來越顯示當前用戶的信息,而不是所有的登記用戶。迭代確實發生,但對於當前登錄的用戶。此外,電子郵件地址未顯示。

助手代碼:

Template.students.helpers({ 
userList: function(){ 
return Meteor.users.find({}); 
}, 

infofullname: function(){ 
return Meteor.user().profile.fullname; 
}, 

infosurname: function(){ 
return Meteor.user().profile.surname; 
}, 

infoemails: function(){ 
return Meteor.user().emails.[0].address; 
} 
}); 

進出口面臨以下問題: 1)中,無法顯示的電子郵件地址。 2)所有用戶的信息沒有得到顯示。

謝謝。

+0

你能告訴你的出版物?這就是將數據傳遞給客戶端以顯示的。 –

+0

@MichelFloyd我還沒有創建出版物。出版物如何在這裏有用?如何爲這種情況下定義出版物?我對流星很陌生。 –

回答

0

發佈的所有用戶在服務器上執行以下操作:

Meteor.subscribe('allUsers'); 

你的助手將需要稍作修改然而由於@Sudhanshu建議:

Meteor.publish('allUsers',function(){ 
    return Meteor.users.find({},{fields: {emails: 1, profile: 1}}); 
    this.ready(); 
}); 

然後在客戶端上使用訂閱由於您正在循環播放用戶的光標,因此您可以利用this作爲循環內的單個用戶對象。

Template.students.helpers({ 
    userList() { 
    return Meteor.users.find({}); 
    }, 

    infofullname() { 
    return this.profile.fullname; 
    }, 

    infosurname() { 
    return this.profile.surname; 
    }, 

    infoemails: function(){ 
    return this.emails.[0].address; 
    } 
}); 

您也可以訪問嵌套的直接火焰屬性,從而避免了三你的助手的,例如:

{{#each userList}} 
<tbody> 
    <tr> 
    <th scope="row">*</th> 
    <td>{{profile.fullname}}</td> 
    <td>{{profile.surname}}</td> 
    <td>{{emails.[0].address}}</td> 
    </tr> 
</tbody> 
{{/each}} 
+1

謝謝你幫助我。現在,即使在刪除自動發佈軟件包後,該應用程序也會返回用戶信息。此外,要獲得電子郵件地址,它應該是:'​​{{emails。[0] .address}}' –

0

多的東西是錯誤的:

  1. Meteor.users()會給你多個用戶只有當你把它們發佈(或者你使用autopublish)。

  2. Meteor.user()將永遠只給你當前登錄的用戶。所以你所有的助手都不會按照你的計劃工作。修改它們以使用Meteor.users.findOne({_id: id)})。你總是可以使用帶參數的助手。

  3. 流星默認發佈只有profile而不是emails。因此,您必須在出版物中發佈emails字段。

+0

感謝您清除這些概念。 –