【问题标题】:Meteor return name from previous query来自先前查询的流星返回名称
【发布时间】:2016-01-29 04:24:56
【问题描述】:

我正在做一个小应用程序,我在其中显示朋友请求和每个请求旁边的“接受/拒绝”按钮。

这是我的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。我想做的是显示请求用户的名字和姓氏,但我不知道从哪里开始。

当然,我尝试在Template helpers 上用{{profile.firstname profile.lastname}}return Meteor.users.find({}); 替换{{fromUser}},但它不起作用。谁能帮我这个?任何帮助将不胜感激。

【问题讨论】:

  • 您能发布一下您的通知集合的样子吗?只是toUserfromUser吗?
  • 是的,它只包含 toUserfromUsercreatedAt

标签: javascript html mongodb meteor


【解决方案1】:

您需要一个助手来查找其他用户文档并返回适当的值:

Template.notifications.helpers({
  fromUserName: function(){
    var fromUser = Meteor.users.findOne({ _id: this.fromUser });
    if ( fromUser ){
      return fromUser.profile.firstname + ' ' + fromUser.profile.lastname;
    }
  }
});

请注意,如果您已删除 autopublish,您还必须(至少)从服务器的用户集合中发布配置文件字段,然后在客户端上订阅该字段。

【讨论】:

  • 那么你将如何在blaze 上呈现它?
  • {{fromUserName}}notifications 模板中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多