Users give an input like "dog, cat" or "cat, dog, fish... etc". Then I need to loop through that input and query the database which has all types of animal food to get the search result respectively for dog and cat.
I want to show the result like this on my handlebars template engine:
Dog- Canine
- Happy Dog Food: 10
- Healthy Dog Food: 15
Cat- Feline
- Kitty Fish Can: 8
List goes on based on user inputs.
router.get('/search', (req, res) => {
let { term } = req.query;
term = term.replace(" ", "").split(',');
let results = {};
parent_query_base = "SELECT * FROM animal_profiles WHERE code = :code"
child_base_query = "SELECT * FROM food f JOIN animal_profiles a ON f.animal = a.animal WHERE a.code = :code"
for (i = 0; i < term.length; i++){
results[db.query(parent_query_base, {replacements: {code: term[i]}, type: QueryTypes.SELECT})] = db.query(child_base_query, {replacements: {code: term[i]}, type: QueryTypes.SELECT});
}
res.render('my_view', {results} );
Handlebars view:
<h1>Seach Results</h1>
{{#each results}}
<h4>{{animal}} - {{species}}</h4>
{{#each food}}
<div>
<h3>{{food_name}}: {{food_price}}
</div>
{{/each}}
{{else}}
<p>No food available</p>
{{/each}}