gpt4 book ai didi

javascript - 发布/订阅 Meteor 返回整个集合

转载 作者:行者123 更新时间:2023-11-30 12:07:06 26 4
gpt4 key购买 nike

我正在为我的 Meteor 应用程序实现发布/订阅架构。我还没有删除 autopublish。我将 React 和 kadira:flow-routerkadira:react-layout 结合使用。它工作正常。

这是我的发布代码(/lib/publications.js 虽然我也尝试使用 /server/publications.js,但没有变化):

if (Meteor.isServer) {
Meteor.publish("games", function() {
return Games.find({});
});

Meteor.publish("game", function(gameId) {
return Games.find({_id: gameId});
});

Meteor.publish("messages", function(gameId) {
return Messages.find({gameId: gameId}, {sort: {createdAt: -1}});
});
}

当然,如果我在 /server 中使用它,我不会添加 if 语句。我像这样订阅路由器中的那些出版物(/lib/routing.js):

user.route('/game/:id', {
name: 'game',
subscriptions: function(params, queryParams) {
this.register('messages', Meteor.subscribe('messages', params.id));
},
// ...
});

然后我像这样在 React 组件中获取数据 (/client/components/pages/game_page.jsx):

// ...
mixins: [ReactMeteorData],
getMeteorData: function() {
return {
messages: Messages.find({}).fetch()
}
},
// ...

我不是获取所有具有 gameId 等于传递的参数的消息,而是获取所有游戏的所有消息。如果我删除发布/订阅并只要求这样的数据:

// ...
mixins: [ReactMeteorData],
getMeteorData: function() {
return {
messages: Messages.find({gameId: gameId}, {sort: {createdAt: -1}}).fetch()
}
},
// ...

它工作得很好。知道为什么吗?我想我遗漏了一些东西:关于发布/订阅本身或关于在路由器中使用订阅。

最佳答案

您必须删除 autopublish 包,因为默认情况下它会发布集合中的所有内容。

据我了解,每个订阅都会公开集合中的一些文档,因此如果您对同一个集合有两个订阅,那么当您查询您的集合时,您将可以访问两个订阅公开的所有文档,因此它是累积的。

关于javascript - 发布/订阅 Meteor 返回整个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34881543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com