Meteor 发布未正确更新?
我在获取Meteor.publish以更新以响应更改的表单字段时遇到问题。对publish的第一个调用似乎仍然存在,因此查询将在该子集中运行,直到重新加载页面为止 我在年遵循了这一方法,但一点运气都没有 非常感谢您的帮助 在lib中:Meteor 发布未正确更新?,meteor,Meteor,我在获取Meteor.publish以更新以响应更改的表单字段时遇到问题。对publish的第一个调用似乎仍然存在,因此查询将在该子集中运行,直到重新加载页面为止 我在年遵循了这一方法,但一点运气都没有 非常感谢您的帮助 在lib中: SearchResults = new Meteor.Collection("Animals"); function getSearchResults(query) { re = new RegExp(query, "i"); return Search
SearchResults = new Meteor.Collection("Animals");
function getSearchResults(query) {
re = new RegExp(query, "i");
return SearchResults.find({$and: [ {is_active: true}, {id_species: {$regex: re}} ] }, {limit: 10});
}
在客户端:
Session.set('query', null);
Template.searchQuery.events({
'keyup .query' : function (event, template) {
query = template.find('.query').value
Session.set("query", query);
}
});
Meteor.autosubscribe(function() {
if (Session.get("query")) {
Meteor.subscribe("search_results", Session.get("query"));
}
});
Template.searchResults.results = function () {
return getSearchResults(Session.get("query"));
}
在服务器上:
Meteor.publish("search_results", getSearchResults);
模板:
搜寻动物
更新[右]
结果是,RegExp
/$regex
存在问题。解释。而不是:
function getSearchResults(query) {
re = new RegExp(query, "i");
return SearchResults.find({$and: [ {is_active: true}, {id_species: {$regex: re}} ] }, {limit: 10});
}
目前,我们需要这样做:
function getSearchResults(query) {
// Assumes query is regex without delimiters e.g., 'rot'
// will match 2nd & 4th rows in Tim's sample data below
return SearchResults.find({$and: [ {is_active: true}, {id_species: {$regex: query, $options: 'i'}} ] }, {limit: 10});
}
那很有趣
PS—具有一些ObjectId功能(
SearchResults=new Meteor.Collection(“Animals”,{idGeneration:“MONGO”});
)以下是我的工作示例:
更新给出的原始javascript是正确的。正如评论中指出的那样,问题在于
HTML:
蒂姆,谢谢你的回复。上面的主要变化(除了合并文件)似乎是从服务器端删除查询选择器。如果我没有弄错的话,这会产生发布整个收藏的效果,也就是说,就像我打开了autopublish一样。不幸的是,动物收藏中大约有50万份文件(收藏名称更改以保护无辜者)。这种方法不会使我的浏览器崩溃吗?哦,谢谢你的澄清。可以我修改了我原来的例子。你的第一次尝试实际上比我意识到的要近:)你能让我知道你的大唱片集是什么样子吗?谢谢你的跟进-刚刚尝试过。结果是一样的(实际上有点糟糕,因为查询为null的检查已取消,所以它返回前10条记录,其中包含一个空白查询)。我不确定上面的代码为什么会产生任何不同,因为现在唯一的变化似乎是将getSearchResults重命名为_get,并从publish间接调用该函数。我相信这很神奇,但此刻流星让我发疯了!现在我重写了它,它看起来和你的非常相似:)我在我的_get函数中放了一个
控制台.log
行,每次我在浏览器中设置键时,它都会重新运行发布函数。你没有看到同样的东西吗?我这样做了:Meteor.publish(“搜索结果”,函数(q){console.log(“发布查询是:+q);结果=_get(q);console.log(“发布结果计数是:+result.count());返回结果;})代码>-查询计数返回正确的外观值,但模板中的结果列表不会刷新。可能是像模板错误这样简单的问题吗?
function getSearchResults(query) {
re = new RegExp(query, "i");
return SearchResults.find({$and: [ {is_active: true}, {id_species: {$regex: re}} ] }, {limit: 10});
}
function getSearchResults(query) {
// Assumes query is regex without delimiters e.g., 'rot'
// will match 2nd & 4th rows in Tim's sample data below
return SearchResults.find({$and: [ {is_active: true}, {id_species: {$regex: query, $options: 'i'}} ] }, {limit: 10});
}
<body>
{{> searchQuery }}
{{> searchResults}}
</body>
<template name="searchQuery">
<form>
<label>Search</label>
<input type="text" class="query" />
</form>
</template>
<template name="searchResults">
{{#each results}}
<div>
{{id_species}} | {{name}} - {{_id}}
</div>
{{/each}}
</template>
Animals = new Meteor.Collection("Animals");
function _get(query) {
re = new RegExp(query, "i");
console.log("rerunning query: " + query);
return Animals.find({$and: [ {is_active: true}, {id_species: {$regex: re}} ] }, {limit: 10});
};
if (Meteor.isClient) {
Session.set("query", "");
Meteor.autosubscribe(function() {
Meteor.subscribe("animals", Session.get("query"));
});
Template.searchQuery.events({
'keyup .query' : function (event, template) {
query = template.find('.query').value
Session.set("query", query);
}
});
Template.searchResults.results = function () {
return _get(Session.get("query"));
}
}
if (Meteor.isServer) {
Meteor.startup(function() {
if (Animals.find().count() === 0) {
Animals.insert({name: "panda", is_active: true, id_species: 'bear'});
Animals.insert({name: "panda1", is_active: true, id_species: 'bearOther'});
Animals.insert({name: "panda2", is_active: true, id_species: 'bear'});
Animals.insert({name: "panda3", is_active: true, id_species: 'bearOther'});
}
});
Meteor.publish("animals", _get);
}