Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
      });
   });
like image 992
Pradeep Saini Avatar asked Nov 09 '22 10:11

Pradeep Saini


1 Answers

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);
  });
});
like image 139
hwillson Avatar answered Nov 14 '22 22:11

hwillson