【发布时间】:2017-12-07 14:01:06
【问题描述】:
我正在发布我所有的 cmets 收藏:
Meteor.publish('comments', function() {
return Comments.find(
{},
{ fields: { date: 1, body: 1, user: 1, attachedTo: 1, attachedToID: 1 } },
);
});
然后订阅一个 React 组件:
export default withTracker(() => {
let commentsSub = Meteor.subscribe('comments');
return {
comments: Comments.find({}).fetch(),
};
})(MyReactComponent);
到目前为止,这可行,但现在我想改进发送的 cmets。 cmets 的 attachTo 字段可以是“profile”或“homepage”。我已经修改了我的 pub 函数来接受输入:
Meteor.publish('comments', function(attachedTo) {
return Comments.find(
{ attachedTo: attachedTo },
{ fields: { date: 1, body: 1, user: 1, attachedTo: 1, attachedToID: 1 } },
);
});
但我看不到如何从我的 React 组件传递这个参数:
export default withTracker(() => {
let commentsSub = Meteor.subscribe('comments');
return {
comments: Comments.find({ attachedTo: 'group' }).fetch(),
};
})(MyReactComponent);
【问题讨论】:
-
在您当前的实现中,发布函数中的
attachTo将是未定义的,因为您没有向它传递任何参数。let commentsSub = Meteor.subscribe('comments','group');您必须将“组”作为参数传递给发布函数。