Thursday, June 9, 2016

Meteor Template undefined

I was trying to invoke a function from within a template. For example in html file i had the following:




 <template name="docTypesTemplate">  
               {{#each types}}  
                 do something ...  
               {{/each}}    
 </template>  


And in my JS file I had:


 Template.body.helpers({    
   types() {    
     return DocTypes.find({});  
   },   
 });  

Sounds pretty straight forward right? You'd expect that you would be able to use the types() function inside your template? WRONG! Instead you will that your variables are 'undefined'.

It turns the types() function is meant to be used within the body of the html ONLY.
So if you want to use the types() function from within a template, you will need to redefine it again as follows:


 Template.registerHelper("types", function() {      
   return DocTypes.find({});    
 });  

I hope this helps somebody else out there as this took me a while to figure out

Cheers!

No comments:

Post a Comment