【问题标题】:No search results while publish and subscribe from single collection从单个集合发布和订阅时没有搜索结果
【发布时间】:2015-10-17 03:38:42
【问题描述】:

我正在使用 Easy-Search 软件包并想搜索帖子(或这些帖子的 cmets)。

问题:输入搜索时没有显示任何内容。console.log 和服务器上都没有显示错误消息。

更新: 我在发布和订阅上都做了 console.log。因此,订阅在浏览器 devtools 上返回 console.log,但发布在服务器终端上没有返回任何内容。

模板 html

<template name="searchPosts">
  {{> esInput id="main" index=indexes placeholder="Search.." }}  

    {{#esEach index="posts"}}
      {{> postItem}}
    {{/esEach}}

    {{#esEach index="comments"}}
      {{> postItem}}
    {{/esEach}}
</template>

模板 js - 客户端

Template.searchPosts.onCreated(function () {
  var self = this, instance;

  instance = EasySearch.getComponentInstance(
    { id: 'main', index: 'posts'},
    { id: 'main', index: 'comments'}
  );
  instance.on('searchingDone', function (searchingIsDone) {
    searchingIsDone && console.log('I am done!');
  });
  instance.on('currentValue', function (val) {
    console.log('The user searches for ' + val);
  });
  this.subscribe("allDocs");
}); 

Template.searchPosts.helpers({
  indexes : function () {                 
    return ['posts', 'comments']; 
  },
  posts: function () {    
    return Posts.find();
  }
});

【问题讨论】:

  • 你用的是铁路由器吗?你忘记订阅路线了吗??
  • 请把订阅放在Route.route 。它已经解决了我的问题几次它应该类似于Router.route('/allDocs', { name: 'allDocs', subscriptions: function () { this.subscribe('allDocs'); }})
  • Router.route('/allDocs', { name: 'allDocs', subscriptions: function () { this.subscribe('allDocs'); }})
  • 那我需要更换助手吗?有 1 个发布/订阅帖子,但我的搜索包括 PostsComments 收藏? posts: function () { return Posts.find(); }.
  • 否。您不必更改助手。 Subsription 将根据不在模板上的路由激活。例如。您可以在 2 条不同的路线上使用相同的模板,并且根据路线订阅,您将获得模板上的数据。助手查询结果将始终是发布查询结果的子集(在您的情况下与没有查询参数相同)。

标签: javascript meteor publish-subscribe


【解决方案1】:

您是否尝试过 initEasySearch 而不是 createSearchIndex?我用过这个,一切都正常显示:

News.initEasySearch(['title', 'body'], {
'limit': 3,
'use': 'mongo-db',
'returnFields': ['title', 'category', 'slug', 'coverImage', 'publishedAt']
})

这将在标题和正文字段上搜索新闻集合,返回下面的字段。然后,在 html 上,我只调用 {{title}} 和其余字段,因为 returnFields 确实是 easySearch 订阅以便在前端显示它的东西。

好的,这是一个工作演示: https://jsfiddle.net/nke3qbxr/

【讨论】:

  • 我正在搜索 2 个集合。我不认为 initEasySearch 允许单个输入跨多个集合或索引进行搜索?如果有,你能包括你的代码吗?截至目前,该网站已关闭
  • 好的,我将在 js fiddle 上添加一个虚拟演示,给我几分钟时间
  • 我编辑了上一篇文章,解释了 js fiddle
猜你喜欢
  • 2013-11-26
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多