【发布时间】:2020-01-02 08:41:06
【问题描述】:
我正在尝试按照 documentation 和示例将服务器端选择器添加到我的 Meteor 应用程序中的搜索功能,使用 Easy Search 插件实现。最终目标是确保仅通过搜索返回用户有权查看的文档。
我可以在 Leaderboard 示例中看到一个选择器,但我无法让它在我的代码中工作。
版本:
Meteor 1.7.0.1
easy:search@2.2.1
easysearch:components@2.2.2
easysearch:core@2.2.2
我修改了 Meteor 'todos' example app 来演示问题,并修改了my demo code is in a repo。
注意!为了演示问题,您需要在演示应用程序中创建一个帐户,然后创建一个列表并将其设为私有。这会将“userId”字段添加到列表中。
然后您可以通过在主要部分顶部附近的搜索框中键入来搜索列表的名称;搜索结果被写入浏览器控制台。
第一个问题是,如果我从example in the documentation 复制代码,我会看到服务器错误'searchObject is not defined:
从文档复制,导致错误:imports/api/lists/lists.js
export const MyIndex = new Index({
'collection': Lists,
'fields': ['name'],
engine: new MongoDBEngine({
selector(searchDefinition, options, aggregation) {
// retrieve the default selector
const selector = this.defaultConfiguration()
.selector(searchObject, options, aggregation)
// options.search.userId contains the userId of the logged in user
selector.userId = options.search.userId
return selector
},
}),
});
文档中似乎有错误。
使用排行榜示例,下面的代码运行但间歇性地不返回任何结果。例如,如果我有一个名为“我的列表”的列表,并且我输入搜索词“s”,则有时该列表会从搜索中返回,有时则不是。如果我使用 MiniMongo 引擎,一切都会完美运行。
index selector {"$or":[{"name":{"$regex":".*my.*","$options":"i"}}],"userId":"Wtrr5FRHhkKuAcrLZ"}
客户端和服务器:imports/api/lists/lists.js
export const MyIndex = new Index({
'collection': Lists,
'fields': ['name'],
'engine': new MongoDBEngine({
selector: function (searchObject, options, aggregation) {
let selector = this.defaultConfiguration().selector(searchObject, options, aggregation);
selector.userId = options.search.userId;
console.log('index selector', JSON.stringify(selector));
return selector;
}
}),
permission: () => {
return true;
},
});
客户端:imports/ui/components/lists-show.js
Template.Lists_show.events({
'keyup #search'(event) {
console.log('search for ', event.target.value);
const cursor = MyIndex.search(event.target.value);
console.log('count',cursor.count());
console.log('results', cursor.fetch());
},
});
客户端:imports/ui/components/lists-show.html
<input id="search" type="text" placeholder="search..." />
编辑:我认为问题在于,虽然 Minimongo 引擎在客户端上运行,但 MongoDBEngine 在服务器上运行,结果存在时间问题。文档显示使用 Tracker.autorun,但这不适合我的 React / Redux 应用程序。如果我想办法解决问题,我会发布答案 - 我不能是唯一一个试图做这样的事情的人。
【问题讨论】:
标签: search meteor permissions