Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No search results while publish and subscribe from single collection

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();
  }
});
like image 719
Thinkerer Avatar asked Oct 17 '15 03:10

Thinkerer


1 Answers

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/

like image 97
João Vilaça Avatar answered Oct 16 '22 07:10

João Vilaça