Search code examples
meteormeteor-blaze

Meteor Reactive Var Not Being Set


I'm getting Uncaught TypeError: Cannot read property 'chapter' of null when trying to get a Reactive Var value. I'm doing exactly the same thing with Session and it works. So I'm confused as to what I'm going wrong. Perhaps the value is being set at the wrong time?

import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var'
import { Stories } from '/imports/collections/stories.js';


/* Story */
Template.story.onCreated(function(){
  // Set the current chapter
  this.chapter = new ReactiveVar(0);
  this.page = new ReactiveVar(0);
});

Template.story.onRendered(function(){
  /* Show the first fragment */
  page.addEventListener( 'changed', function( event ) {
    console.log( Template.instance().chapter.get() );
    ....

The event is being fired, but the instance of chapter isn't being set in onCreated.

What am I doing wrong here?


Solution

  • try this:

    Template.story.onRendered(function(){
      let instance = Template.instance();
      /* Show the first fragment */
      page.addEventListener( 'changed', function( event ) {
        console.log( instance.chapter.get() );