As a newcomer to Meteor, I am attempting to incorporate a straightforward search feature into the website. While similar inquiries exist on Stack Overflow, none of the solutions have proven to be suitable for my situation.
The search bar is located in the header section. I have successfully subscribed and published a search. However, I am unclear about the next steps. How can I transmit the data from the search bar in order to query the database? What modifications need to be made to the header.js file to enable this database search functionality?
Here is the pertinent code:
header.js
Template.header.events({
'submit form': function(e) {
e.preventDefault();
var query = $(e.target).find('[name=search]').val();
***** What should I add here? *****
Router.go('searchResults');
}
});
search_results.html
<template name="searchResults">
<ul>
{{#each questions}}
<li>{{questionItems}}</li>
{{/each}}
</ul>
</template>
publications.js
Meteor.publish('searchResults', function(searchValue) {
return Questions.find({
body: {
$regex: searchValue
}
});
});
router.js
Router.route('/searchResults', {
name: 'searchResults',
waitOn: function() {
return [Meteor.subscribe('searchResults')];
}
});