Emmanuel Amodu Emmanuel Amodu - 1 month ago 18
Javascript Question

Meteor reactive search code not running

Can someone pls take a look at my code and tell me how to fix it to get it to run, I am trying to create a reactive search for a meteor app but it not working.

Template.search.onCreated(function(){
this.textd = new ReactiveVar('search');
});

Template.search.helpers({
results: function() {
return Websites.find(Template.instance().textd.get());
}
});

Template.search.events({
'keyup .inp': function(event, template) {
template.textd.set(document.getElementById("edValue").value);
}
});

Answer
Template.search.onCreated(function(){
  this.textd = new ReactiveVar('search');
});

Template.search.helpers({
   results: function() {
      //var regexp = new RegExp(Session.get('search/keyword'), 'i');
      //return Websites.find(Template.instance().textd.get());
      let textd = Template.instance().textd.get();
      return Websites.find({
         name: {$regex: textd}
      });
   }
});

Template.search.events({
  'keyup .inp': function(event, template) {
      //Session.set('search/keyword', document.getElementById("edValue").value);
    template.textd.set(document.getElementById("edValue").value);
   }
});

This is what I have done and it seem to be working, fine except I am not satisfied I want to search through the entire field not just one field.

edit

I have been able to do what I want

Template.search.helpers({
  results: function() {
      //var regexp = new RegExp(Session.get('search/keyword'), 'i');
      //return Websites.find(Template.instance().textd.get());
      let textd = Template.instance().textd.get();
      if(textd!==""){
           return Websites.find({ 
              $or: [
                  {title: {$regex: textd}},
                  {url: {$regex: textd}},
                  {description: {$regex: textd}},
                  {createdBy: {$regex: textd}}
              ]
          });
      } else {
        return false;
      }
   }
});
Comments