javascript - 前一个查询的 meteor 返回名称

标签 javascript html mongodb meteor

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

这是我的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/

相关文章:

c# - YUI 按钮启动回发两次

javascript - HTML5/Javascript 从头部到 body 添加值

java - Spring boot/Application.java 如何获取另一个包中的 Mongo AbstractMongoConfiguration ?

php - 在社交网络上存储文件的最佳方式

mongodb - 如何在 Mongo 中对 2 个或多个数组的元素求和

javascript - agora.io web sdk - 获取外部播放器的流链接

jquery - div中的滚动条滚动而不是页面滚动条

javascript - 即使 CSS 发生更改,Jquery 函数也会继续应用。我该如何阻止这个?

javascript - 使用 JavaScript 读取/写入/更改本地文本文件(在 DropBox 上)的内容

javascript - 为什么这个ajax帖子不能正常工作?