Pradeep Saini
Pradeep Saini

Reputation: 740

how to access meteor ReactiveVar variable in meteor call in onRendered

I want to access ReactiveVar variable in onRendered getting error Exception in delivering result of invoking 'getUsersData': TypeError: Cannot read property 'userData' of null

    Template.editAdmin.onCreated(function() {
     this.userData = new ReactiveVar([]);
   });

   Template.editAdmin.onRendered(function() {
     Meteor.call("getUsersData", this.data, function(err, result) {
        Template.instance().editAdminId.set(result);
      });
   });

Upvotes: 5

Views: 1041

Answers (1)

hwillson
hwillson

Reputation: 1399

It doesn't look like your code sample lines up with your error message. You mentioned the error is:

TypeError: Cannot read property 'userData' of null

but from looking at your code sample, I can see the error will really be:

TypeError: Cannot read property 'editAdminId' of null

I'll assume you want it working with userData, so I'll adjust your code accordingly. Basically you want to make sure you're leveraging javascript closure properly when using Template.instance() in your Method callback. For example:

Template.editAdmin.onCreated(function () {
  this.userData = new ReactiveVar([]);
});

Template.editAdmin.onRendered(function () {
  const instance = Template.instance();
  Meteor.call("getUsersData", this.data, function (err, result) {
    instance.userData.set(result);
  });
});

Upvotes: 1

Related Questions