10

Im using Easy-Search package and would like to search posts (or comments to those posts).

The problem: Nothing gets shown when search is typed in. No error messages shown on both console.log and server.

Updates: I did console.log on both the publish and subscribe. So the subscribe returns the console.log on the browser devtools but the publish does not return anything on the server terminal.

Template html

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

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

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

Template js - client

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();
  }
});
Thinkerer
  • 1,606
  • 6
  • 23
  • 43
  • are you using iron Router ? did u forget to subscribe in the route ?? – gatolgaj Oct 21 '15 at 12:24
  • can you please put the subscription in ```Route.route``` . It has solved my problem couple of times it should be Something similar to this ```Router.route('/allDocs', { name: 'allDocs', subscriptions: function () { this.subscribe('allDocs'); }}) ``` – gatolgaj Oct 22 '15 at 07:15
  • ```Router.route('/allDocs', { name: 'allDocs', subscriptions: function () { this.subscribe('allDocs'); }})``` – gatolgaj Oct 22 '15 at 07:22
  • do I need to change the helper then? theres 1 pub/sub to Posts but my search includes both `Posts` and `Comments` collections?. `posts: function () { return Posts.find(); }`. – Thinkerer Oct 22 '15 at 07:25
  • No .You dont have to change the Helper. the Subsription will be active based on the route not on the template. For example. you may use the same template on 2 different routes , and based on the routes subcription you will get the data on the template. Helper Query result will always be a subset of Publish query result(in your case same as there is no query parameters). – gatolgaj Oct 22 '15 at 07:31
  • the above doesnt work, neither does wrapping it in the autorun work either – Thinkerer Oct 22 '15 at 17:02
  • What do you mean, @gatolgaj? If you do use the same template on other routes, the logic to subription should be the same anyway so the subscription upon rendering is totally ok. – durrrr Oct 26 '15 at 08:02
  • Hi all, I have requested for this question to be removed as it is no longer relevant. The easy-search is now deprecated for a newer version 2.0.0 which is very different in terms of implementation. – Thinkerer Oct 26 '15 at 10:28
  • Its not Necessary that subscription to the template need to be the same. For example let say i have a template ```ShowComment```, Which displays the Comments. This template can be used for showing all the Comments on a post. as well as to show only comments done given by a specific user based on the Route. – gatolgaj Oct 26 '15 at 15:55
  • While this is true, there are ways to manipulate the subscription query. Anyway in this case here, this isn't relevant as everything gets published. – durrrr Oct 27 '15 at 06:24

1 Answers1

1

Have you tried initEasySearch instead of createSearchIndex? I've used this and everything is showing properly:

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

This will search the News collection , on the fields title and body, returning those fields below. Then , on the html, i just call {{title}}, and the rest of the fields, because the returnFields are really the things that easySearch subscribe in order to display it on the frontend.

ok, here's a working demo: https://jsfiddle.net/nke3qbxr/

João Vilaça
  • 601
  • 1
  • 6
  • 13