Meteor ReactiveVar - TypeError:无法调用未定义的方法'set'

时间:2022-09-29 19:42:02

I tried to use ReactiveVar. I doesnt know how to handle ReactiveVar. Here the code I have tried.

我尝试使用ReactiveVar。我不知道如何处理ReactiveVar。这里是我试过的代码。

Template.Home.helpers({
  names: function(){
    temp = Template.instance().name.get();
    return temp;
  }
});

Template.Home.onCreated(function () {
  this.name = new ReactiveVar();
  Meteor.call("getNames", function(error, result) {
    if(error){
      alert("Oops!!! Something went wrong!");
      return;
    } else {
      this.name.set(result); // TypeError: Cannot call method 'set' of undefined
      return;
    }
  });
});

Is am I right to set and get ReactiveVar? or How to set and get the ReactiveVar ??

我是否正确设置并获得ReactiveVar?或者如何设置和获取ReactiveVar?

1 个解决方案

#1


Your logic is correct, your error is actually a common JS pitfall : inside the Meteor.call callback function, this scope is modified and no longer references the template instance.

你的逻辑是正确的,你的错误实际上是一个常见的JS陷阱:在Meteor.call回调函数中,这个范围被修改,不再引用模板实例。

You need to use Function.prototype.bind and update your code :

您需要使用Function.prototype.bind并更新您的代码:

Template.Home.onCreated(function () {
  this.name = new ReactiveVar();
  Meteor.call("getNames", function(error, result) {
    if(error){
      alert("Oops!!! Something went wrong!");
      return;
    }
    this.name.set(result);
  // bind the template instance to the callback `this` context
  }.bind(this));
});

You could also use a local variable captured by a closure (you'll often see this style in JS projects) :

您还可以使用闭包捕获的局部变量(您经常在JS项目中看到这种样式):

Template.Home.onCreated(function () {
  // use an alias to `this` to avoid scope modification shadowing
  var template = this;
  template.name = new ReactiveVar();
  // the callback is going to capture the parent local context
  // it will include our `template` var
  Meteor.call("getNames", function(error, result) {
    if(error){
      alert("Oops!!! Something went wrong!");
      return;
    }
    template.name.set(result);
  });
});

#1


Your logic is correct, your error is actually a common JS pitfall : inside the Meteor.call callback function, this scope is modified and no longer references the template instance.

你的逻辑是正确的,你的错误实际上是一个常见的JS陷阱:在Meteor.call回调函数中,这个范围被修改,不再引用模板实例。

You need to use Function.prototype.bind and update your code :

您需要使用Function.prototype.bind并更新您的代码:

Template.Home.onCreated(function () {
  this.name = new ReactiveVar();
  Meteor.call("getNames", function(error, result) {
    if(error){
      alert("Oops!!! Something went wrong!");
      return;
    }
    this.name.set(result);
  // bind the template instance to the callback `this` context
  }.bind(this));
});

You could also use a local variable captured by a closure (you'll often see this style in JS projects) :

您还可以使用闭包捕获的局部变量(您经常在JS项目中看到这种样式):

Template.Home.onCreated(function () {
  // use an alias to `this` to avoid scope modification shadowing
  var template = this;
  template.name = new ReactiveVar();
  // the callback is going to capture the parent local context
  // it will include our `template` var
  Meteor.call("getNames", function(error, result) {
    if(error){
      alert("Oops!!! Something went wrong!");
      return;
    }
    template.name.set(result);
  });
});