Meteor reactive search code not running


#1

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() {
        //var regexp = new RegExp(Session.get('search/keyword'), 'i');
        return Websites.find(Template.instance().textd.get());
    }
});

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 how far I have come, I even tried using sessions instead of reactive-var package at first non worked.


#2

So it looks like your query isn’t built right. If you wanted to query for results matching the exact name:

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: textd
        });
    }
});

#3

Thanks @vigorwebsolutions, It fixes it. just one more question how do I then display result for db elements that matches text, for example, return all text that contains a string that is the search input for example, show “I am an apple”, when “apple” is keyed up

edit

I just did a google search and I found this

db.database.find({A: /abc def/i })

Pls can you tell me how to implement this in my code if you don’t mind.

edit

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({
            title: {$regex: textd}
        });
    }
});

I think I have done. One more question pls how can I search all


#4

If you mean searching multiple fields with regex, I use something like this:

let textd = Template.instance().textd.get();
return Websites.find({
  $or: [{
    name: {
      $regex: textd,
      $options: 'i'
    },
  }, {
    url: {
      $regex: textd,
      $options: 'i'
    },
  }, {
    description: {
      $regex: textd,
      $options: 'i'
    },
  },]
});

#5

That is even better thank you