我正在做一个小应用程序,我在其中显示好友请求和每个请求旁边的“接受/拒绝”按钮。
这是我的Template.notifications.helpers
:
listRequests: function(){
return Notifications.find({toUser: Meteor.userId()});
}
这是我的通知(我在其中显示好友请求通知)模板:
<template name="notifications">
{{#each listRequests}}
<p>{{displayUserName}}
<span id="fromUserId"><strong>{{fromUser}}</strong></span> sent you a friend request.
<button class="btn btn-primary" id="btnAcceptRequest">Accept</button>
<button class="btn btn-danger" id="btnRejectRequest">Reject</button>
</p>
<p>{{createdAt}}</p>
{{/each}}
</template>
这是我的 user
集合:
{
"_id": "zaSuTBgRh3oQcPSkh",
"emails": [
{
"address": "johnsmith@yahoo.com",
"verified": false
}
],
"profile": {
"firstname": "John",
"lastname": "Smith"
}
}
目前,此代码有效。问题是它只显示发送请求的用户的 _id
,即 fromUser
。我想做的是显示请求用户的名字和姓氏,但我不知道从这里去哪里。
当然,我尝试用 {{profile.firstname profile.lastname}}
替换 {{fromUser}}
并 return Meteor.users.find( {});
在 Template helpers
上,但它不起作用。谁能帮我这个?任何帮助将不胜感激。
最佳答案
您需要一个助手来查找其他用户文档并返回适当的值:
Template.notifications.helpers({
fromUserName: function(){
var fromUser = Meteor.users.findOne({ _id: this.fromUser });
if ( fromUser ){
return fromUser.profile.firstname + ' ' + fromUser.profile.lastname;
}
}
});
请注意,如果您删除了自动发布,您还必须(至少)从服务器的用户集合中发布个人资料字段,然后在客户端订阅该字段。
关于javascript - 前一个查询的 meteor 返回名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35063256/