Search code examples
meteormeteor-blazemeteor-helper

Meteor ReactiveVar doesnt works with Meteor.call in event


I am using ReactiveVar. onCreated I set ReactiveVar with Meteor.Call. When I try to change my reactive variables from event, I am getting Exception in delivering result of invoking 'namelist' Hope my code below will explain my problem more.

Template:

   <template name="Name">
    {{#each list}}
      Name : {{name}}
      Age : {{age}}
    {{/each}}
    <button class="loadmore">Load More</button>
    <template>

Helper:

Template.Name.helpers({
  'list': function(){
    return Template.instance().list.get();
  }
})

OnCreated:

Template.Name.onCreated(function () {
  this.limit = new ReactiveVar(5);
  this.skip = new ReactiveVar(0);
  this.list = new ReactiveVar();

  Meteor.call("namelist", Template.instance().skip.get(), Template.instance().limit.get(), function(error, result) {
    if(error){
      alert("Oops!!! Something went wrong!");
      return;
    } else {
      this.list.set(result);
      this.skip.set(this.limit.get());
      this.limit.set(this.limit.get()+5);
      return;
    }
  }.bind(this));

});

Event :

Template.Name.events({
  'click .loadmore': function(event, template) {

    Meteor.call("namelist", template.skip.get(), template.limit.get(), function(error, result) {
      if(error){
        alert("Oops!!! Something went wrong!");
        return;
      } else {
        temp = template.list.get();
        temp.push(result);
        template.list.set(temp); // Here I am getting error of Exception in delivering result of invoking 'namelist'
        template.skip.set(template.limit.get());
        template.limit.set(template.limit.get()+5);
        return;
      }
    });
  }
});

Solution

  • Without executing the code I can't tell precisely what's going on, but it may be that your initial Meteor.call isn't setting list to be an array. Additionally, temp is declared without var so it's leaking into the global namespace. In your event handler you could try writing this:

    var temp = template.list.get() || [];
    temp.push(result);
    template.list.set(temp);