如何在流星中获取其他用户的个人资料详细信息

How can I get other users' profiles details in meteor

我在访问当前用户以外的用户的用户个人资料详细信息时遇到问题。

目标是在博客条目列表中的每个 post 下方显示一个小页脚。页脚应包含 post 和作者详细信息(如日期、用户名等)。

博客条目由作者的 _id 标识,但关键是我无法访问

Meteor.users.find({_id : authorId});

生成的游标似乎与 Meteor.user(不是 'users')相同,并且仅包含一个文档,并且仅对当前用户 ID 有效。对于其他的,比如作者ID,我只能得到一个空集合。

问题是,除了下一个 Meteor.user 订阅获取作者个人资料(如用户名 profile.nick 等)之外,还有什么办法吗???

更新: 如果您想在单个订阅中获得博客条目和用户详细信息,您可以 Publish Composite 打包。请参阅以下示例代码并根据您的集合架构进行编辑,

Meteor.publishComposite('blogEntries', function (blogEntryIds) {
    return [{
        find: function() {
            return BlogEntries.find({ courseId: { $in: blogEntryIds }});
            // you can also do -> return BlogEntries.find();
            // or -> return BlogEntries.find({ courseId: blogEntryId });
        },
        children: [{
            find: function(blogEntry) {
                return Meteor.users.find({ 
                    id: blogEntry.authorId 
                }, { 
                   fields: { 
                        "profile": 1,
                        "emails": 1
                   } 
                });
            }
        }}
    }]
});

更新结束

您需要从服务器发布 Meteor.users 才能在客户端上使用它。 accounts 软件包将发布当前用户,这就是为什么您只能看到当前用户的信息。

在服务器文件夹或 Meteor.isServer if 块中的文件中执行类似这样的操作

//authorIds = ["authorId1", "authorId2];
Meteor.publish('authors', function (authorIds) {
    return Meteor.users.find({ _id : { $in: authorIds }});
});

Meteor.publish('author', function (authorId) {
    return Meteor.users.find({ _id : authorId });
});

然后在客户端订阅这个发布,在模板的 onCreated 函数中,像这样

Meteor.subscribe('author', authorId); //or Meteor.subscribe('author', authorIds);

template.subscribe('author', authorId); //or template.subscribe('author', authorIds);

如果您只想显示用户名(或其他几个字段),您可以将它们与 authorId 一起保存在 post 文档中。例如:

post:{
   ...
   authorId: someValue,
   authorName: someValue
} 

您可以在模板中将它们用作 post 的字段。 如果您不想在 post 文档中嵌入太多字段(因此您只想保留 authorId),则可以在发布 post 时使用 publish-composite . (参见示例 1)

您不需要发布所有用户及其个人资料。