Access to data in different parts of a template



I used to subscribe to the data from the router part. But I wanted to move that part to the template part. But I can’t found a way to access to the data from different parts.

In the HTML part, I found that the best way is to do a helper… OK

In the template I always subscribe to the data in the created part. Like this

Template.sessionDate.created = function() {
  this.autorun(() => {
    const sessionID = Router.current().params._ID;
    this.sessionDate = {};
    this.subscribe( '', sessionID );

But how to access the data (sessionDate here) from the helpers and the events parts?



I see you are using ES6 arrow function and const, so must be using a recent version of Meteor.
You should use instead of

To access the data of a Template, you can directly use your data keys on your template Object (for example MyTemplateObject.myData)

For events, you can get this Object by using the second parameter of the function, and for helpers by using Template.instance(). See steps 7.3, 7.4 and 7.5 of this tutorial.

You also have Template.currentData() or <templateInstance>.data but I think, in your case, that it is not what you are looking for.


Hi @borntodev!
Yes exactly, I have to migrate the onCreated declaration.

For the data in the template, I’m still not sure what todo. I understand how to retrieve it know, thanks to you but where do I have to define the data template and how?


Which router are you using?
Anyway, you have to use a Collection within a helper and return the data from the helper, then call the helper in the template.


I’m using IronRouter. I want to switch to FlowRouter. Is for that reason, I’m putting the subscriptions in the template. I used to put the data from the router in the router part but now that I switch to template subscription I don’t see how to add the data to the template…


Subscribe at onCreated

Template.Layout_protected.onCreated(function () {

Query for data in helper, usually call find or findOne method from a collecction in the helper definition:

  currentUserEmail() {
    let user = Meteor.user();
    return user && && &&;


It’s exactly what I did. But I get a problem to access a data from a event for example. I can copy/paste the helper that get my data but it might be an easiest solution to get the data in a template event function, no?


Like you did, in the .onCreated(), is correct.


But my sessionDate data is empty in the onCreated… When and where I can fill it?
Or maybe I’m missing something :thinking:


Your data is empty because you are assigning an empty Object.

this.sessionDate = {};

I think you should move this statement outside your .autorun().


I already answered to that : see my first reply. (I just rephrased it a bit)


Thanks @borntodev for your patience :stuck_out_tongue:
Is that portion of code good?

Template.sessionDate.onCreated(function () {
  this.sessionID = Router.current().params._ID;
  this.autorun(() => {
    this.subscribe( '', this.sessionID );
  this.sessionDate = getSessionDateInfos(this.sessionID); // Is the data subscribed here??

Thanks :+1:


In this case no, if you want to update sessionDate each time _ID is changed you need to keep it in your .autorun() . (I though you just wanted to initialise an empty Object, sorry).

Template.sessionDate.onCreated(function () {
  this.autorun(() => {
    // the function will rerun if it depends on reactive data sources
    // see :

    this.sessionID = Router.current().params._ID;
    this.subscribe( '', this.sessionID );
    this.sessionDate = getSessionDateInfos( this.sessionID );

If you only use sessionID in your .autorun(), save it with const sessionID = ... . (like you did before), no need to bind it to the template if you do not reuse it elsewhere.


To check when a .subscribe() is done, you can add a callback in the parameters :


Perfect! I will refactor my templates in that way!