Template.helpers using sessions


#1
I have a problem using session in Template.helpers.
I have a template."home" with 2 selects dropdown

<template name="home>
	<select class="custom-select" name="category" id="category" >
                <option selected>Choose...</option>
                {{#each category}}
                    <option >{{name}}</option>
                  {{/each}} 
              </select>

	<select class="custom-select" name="sub-category" id="sub-category" >
                <option selected>Choose...</option>
                {{#each sub}}
                    <option >{{name}}</option>
                  {{/each}} 
              </select>
</template>

Now in my home.js file I want to populate these two with Template.helpers (imports and subscribes not shown - but working ok)


	Template.home.helpers({
	category: function(){
	return Categories.find({}, {
	sort: { name: 1 }
	});
	},
	});

	Template.auctions_lots.helpers({
	sub: function(){
	const value = Session.get(“subcategory”);
	return value.find({}, {
	sort: { name: 1 }
	});
	}
	});
I`am populating the Categories.find from a collection Categories and I have 10 other collections with names corresponding to the fields in Categories.Collection.
So the idea is to grab the selected value(name) i.e. "PETS" in the first helper, store it in a session and use it as the collection.find -  PETS.find 
in the second helper. Like so :
	
	Template.home.onRendered(function () {
	('#category').change(function () { var selectedText = (this).find(‘option:selected’).text();
	$(".test").text(selectedText);
	Session.set(“subcategory”, selectedText);
	});
	});

the Session is working fine - (console.log())- and if i put in PETS.find its also Ok - but the Template.helper wont read Session.get.
Any help to make me understand ?



#2

Welcome to the Meteor forums, @fna.

To make your code more readable and help us answer your question, please edit your post and wrap your code blocks in line with triple backticks:

```
put
code
here
```

Thanks :slight_smile: