手动发布/订阅场景中可能存在Meteor错误
我在Meteor中订阅/发布时遇到问题。我已经编写了一个示例Meteor应用程序来帮助缩小问题的范围 我正在服务器上发布一个集合,该集合由通过客户端上的订阅传入的参数过滤。此订阅位于autosubscribe中,autosubscribe利用会话变量以反应方式更新订阅 当更改此特定会话变量的状态时,客户端上的集合没有得到正确更新,或者至少我收集到的信息是这样的。我花了一整天的时间在这上面,在我控制的代码中没有发现任何问题。我怀疑我要么不了解如何在Meteor中设置合适的酒吧,要么Meteor中存在问题 要重现此问题,请启动一个新的Meteor项目并使用以下命令(请确保在尝试时删除autopublish包): HTML(例如test.HTML):手动发布/订阅场景中可能存在Meteor错误,meteor,Meteor,我在Meteor中订阅/发布时遇到问题。我已经编写了一个示例Meteor应用程序来帮助缩小问题的范围 我正在服务器上发布一个集合,该集合由通过客户端上的订阅传入的参数过滤。此订阅位于autosubscribe中,autosubscribe利用会话变量以反应方式更新订阅 当更改此特定会话变量的状态时,客户端上的集合没有得到正确更新,或者至少我收集到的信息是这样的。我花了一整天的时间在这上面,在我控制的代码中没有发现任何问题。我怀疑我要么不了解如何在Meteor中设置合适的酒吧,要么Meteor中存
<head>
<title>pubsubbug</title>
</head>
<body>
{{> main}}
</body>
<template name="main">
<h1>Example showing possible bug in Meteor wrt pub-sub</h1>
<p><button name="showall">show all ({{showall}})</button></p>
<div style="float:left;width:400px;">
<h2>Notes:</h2>
<ul>
{{#each notes}}
<li>{{title}}</li>
{{/each}}
</ul>
</div>
<div style="float:left;">
<h2>Notes (copied):</h2>
<ul>
{{#each notes_copied}}
<li>{{title}}</li>
{{/each}}
</ul>
</div>
</template>
对您将看到的内容的解释:
将有一个按钮,单击该按钮时,只需在true和false之间切换会话变量(showall)
存在两个订阅(在自动订阅中),一个是举例说明错误,另一个是后缀为\u copied
,这是一个测试,用于证明当有问题的集合被“复制”并且分配了新的uuid时,结果会正确显示。我不知道该怎么处理这些信息。。。我不想要新的uuid
因此,基本上,当反复单击ShowAll按钮时,第一列注意:将显示不正确的结果,几次单击后将不会显示任何内容
另一方面,第二列Notes(copied):正确显示,它的uuid每次都会重新生成
这是虫子吗?还是有一个合适的方法来做到这一点
编辑:上面的示例在现场直播,在Windows上的开发者分支上没有遇到错误。既然是这样,这是一个很好的迹象,表明您的代码没有问题。您似乎看到了一些关于订阅和/或Mongo查询方式的错误
Meteor本身很可能在其主机上运行稳定(=主)版本,因此您必须尝试不同的方法或等待新版本。除非您支持在devel上运行…使用devel branch运行代码,工作正常。对于以后查看此线程的任何人,此错误仅适用于Meteor版本
if (Meteor.is_client) {
Notes = new Meteor.Collection("notes_collection");
NotesCopied = new Meteor.Collection("notes_collection_copied");
Session.set("showall", false);
Meteor.autosubscribe(function () {
Meteor.subscribe("notes_subscription", Session.get("showall"), function () {
console.log("Notes count:", Notes.find().count());
});
Meteor.subscribe("notes_subscription_copied", Session.get("showall"), function () {
console.log("Bug? This isn't getting called.");
console.log("NotesCopied count:", NotesCopied.find().count());
});
});
Template.main.notes = function () {
return Notes.find();
};
Template.main.notes_copied = function () {
return NotesCopied.find();
};
Template.main.showall = function () {
return Session.get("showall");
};
Template.main.events = {
"click button[name='showall']": function (evt) {
Session.set("showall", !Session.get("showall"));
}
};
}
if (Meteor.is_server) {
Notes = new Meteor.Collection("notes_collection");
var getNotes = function (showall) {
if (showall) {
return Notes.find({}, {sort: {title: 1}});
} else {
return Notes.find({visible: true}, {sort: {title: 1}});
}
};
Meteor.publish("notes_subscription", function (showall) {
// By sending the Notes back with the same uuid as before, the
// client end seems to get confused:
return getNotes(showall);
});
Meteor.publish("notes_subscription_copied", function (showall) {
var notes = getNotes(showall);
var self = this;
// Copy notes into a new notes collection (see NotesCopied on client).
// By generating a new uuid, we don't get an issue with the notes
// on the client getting screwed up:
notes.forEach(function (note) {
var uuid = Meteor.uuid(); // note._id will cause same problem
self.set("notes_collection_copied", uuid, {title: note.title});
});
self.flush();
self.complete();
});
// Add example notes
Meteor.startup(function () {
if (Notes.find().count() === 0) {
Notes.insert({title: "Note #1 (always visible)", visible: true});
Notes.insert({title: "Note #2 (always visible)", visible: true});
Notes.insert({title: "Note #3 (always visible)", visible: true});
Notes.insert({title: "Note #4 (only visible when showall true)", visible: false});
Notes.insert({title: "Note #5 (only visible when showall true)", visible: false});
Notes.insert({title: "Note #6 (only visible when showall true)", visible: false});
}
});
}