我能够显示特定用户的帖子列表,但无法显示包含特定用户评论的帖子。
官方链接中的示例使用此层次结构 here
对于我的问题,层次结构是:
all posts ->
(children)
-> find all post by user
-> find all comment by user
(children)
-> find post that has id that matches comments
到目前为止我所拥有的..
发布复合线
Meteor.publishComposite('user', function(_id) {
return {
find: function() {
return Meteor.users.find({_id: _id}, { fields: { profile: 1, username: 1} });
},
children: [
{
find: function(user) { return Posts.find({ userId: user._id }); }
},
{
find: function(user) { return Comments.find({ _id: user._id }); }
children: [
{
find: function(comment) { return Posts.find({ _id: comment.postId }); }
}
]
}
]
};
});
在模板中
Template.usersShow.helpers({
user: function () {
return Meteor.users.findOne({ _id: Router.current().params._id });
}
});
在路由器中
Router.route('/users/:_id', { name: 'users.show',
waitOn:function(){
// console.log(this.params._id);
return Meteor.subscribe('user', this.params._id);
}
});
html
{{#with user}}
<div class>{{ profile.name }}</div> <!-- name appears -->
{{#ionList}}
{{#each post}}
{{> postItem}}
{{/each}}
{{/ionList}}
{{/with}}
最佳答案
我第一眼看到的是:
- 您无需发布
pullData
无论如何,user
出版物可以完成您在 1) 中提到的工作 - 在您的代码中尝试按照以下模式查找
posts
和comments
属于user
:
children: [
{
find: function(user) { return Posts.find({ authorId: user._id }); }
},
{
find: function(user) { return Comments.find({ authorId: user._id }); }
}
]
只需替换 authorId
在 Post
中使用适当的字段名称和Comment
方案。
我找到了publishComposite
在 Meteor 中发布连接的优秀解决方案。
Join in Meteor with publishComposite
希望这有帮助,
亚历克斯
附注我已重新阅读您编辑过的问题,并建议您:
- 将您的应用程序拆分为 View (阅读模板),例如
AllPosts
,MyPosts
TopTenPosts
,PostsByUser
,PostWithComments
,MyCommentedPosts
等等 - 定义每个 View 必须显示的内容,并将内容转换为订阅
- 为每个订阅定义发布(您可能想以某种方式对其进行 stub )
- 确保您的观点符合预期
- 重构出版物,以便它们仅从 mongo 中提取特定 View 所需的数据
TDD 来拯救。
关于javascript - 使用发布复合进行共享连接: pull out user's posts + comments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31699768/