使用MeteorJS堆栈,如何检索用户's的电子邮件,其中包含他们发布的帖子中的userId

Using the MeteorJS stack, how do I retrieve a user's email with their userId from a post they made

本文关键字:包含 电子邮件 他们 userId 何检索 堆栈 MeteorJS 检索 使用 用户      更新时间:2024-07-04

MeteorJS新手。我有一个"posts"集合,其中包含字段:title、createdAt、body、userId。虽然我只有两个用户,但似乎每个帖子都有不同的userId。我基本上只想显示博客文章,以及他们各自用户的电子邮件。我目前的实现如下,它显示的是当前用户的电子邮件,而不是博客文章电子邮件的所有者:

"click .main-feed-post" : function(event) {
...
document.getElementById('post-view-email').innerHTML = Meteor.users.findOne({_id: this.userId}).emails[0].address;
...
}

目前只输出登录用户的电子邮件地址。上一个用于查看个人博客文章。此外,我有一个主要的提要,在那里我列出了所有的博客文章和他们各自所有者的电子邮件:

    {{#each posts}}
        <li class="main-feed-post">
            {{title}}
            <div class="main-feed-post-data">
            <label>BY</label> {{getUserEmail}} <label>AT</label>                                         {{formattedDate}}
    </div>
   </li>

getUserEmail : function() {
        return Meteor.users.findOne({_id: this.userId}).emails[0].address;
}
   {{/each}}

除非是当前用户的电子邮件,否则不会输出任何内容。理想情况下,我会在用户对象中添加一个username字段,并显示它,而不是userId或电子邮件。我不知道如何使用accounts-ui和accounts-password包来实现这一点。欢迎任何帮助!提前感谢!

内部服务器:

   Meteor.publish("allUsers", function () {
    return Meteor.users.find({});
});
Meteor.publish("allUserData", function () {
    return Meteor.users.find({}, {fields: {"emails.address": 1}});
});

内部客户端:

Meteor.subscribe("allUsers");
Meteor.subscribe("allUserData");