Solved: es6 problem with Collection.helpers

Hi guys,

this is probably a total newbie question:

Using es6 I just ran into a problem using collection-helpers:

This is the code I wrote in my .js file, which gives me an TypeError: _this.isPerson is not a function error in the client:

Contacts.helpers({
  isPerson: () => {
    if (this.typeType==='Person') {
      return true;
    }
    return false;
  },
  getPersonAsString: () => {
    if (this.isPerson()) {
       // ...
    }
    return '';
  },

Basically Meteors es6-converter converts this into _this, which is not what I want.

What am I doing wrong??

Using the ()=> syntax is rebinding this. You need to use a regular anonymous function so that this correctly refers to the collection instance.

Change this to

getPersonAsString() {
    if (this.isPerson()) {
       // ...
    }
    return '';
  },

And it should work :smile:

@benoitt: Thanks a lot - it works like a charme and it looks so much better :smiley:

1 Like