Solved: es6 problem with Collection.helpers


#1

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??


#2

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


#3

Change this to

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

And it should work :smile:


#4

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