Angular-meteor using es6 function syntax doesn't work in some places

in the following example this is the Meteor object:

Meteor.publish('events', function() {
    console.log(this)
    return Events.find({
        $or: [
            {
                $and: [
                    {"public": true},
                    {"public": {$exists: true}}
                ]
            },
            {
                $and: [
                    {owner: this.userId},
                    {owner: {$exists: true}}
                ]
            }
        ]
    })
})

while in this example this is something entirely different that doesn’t have the userId parameter:

Meteor.publish('events', () => {
    console.log(this)
    return Events.find({
        $or: [
            {
                $and: [
                    {"public": true},
                    {"public": {$exists: true}}
                ]
            },
            {
                $and: [
                    {owner: this.userId},
                    {owner: {$exists: true}}
                ]
            }
        ]
    })
})

is this a bug?

1 Like

This is not a bug.

An arrow function => is not the same as a regular function() and you can’t just switch one to the other because the arrow changes the meaning of this to be the this from the enclosing scope.

1 Like

Thanks you for the quick answer.
So is there a nice way to send the “this” i want?
in react for example i can bind my function with ‘this’ in order to send it the right context, i.e:

<div onclick="{this.callmyfunc.bind(this)}"></div>

Your first example works already, so just use it :smile:

Just because arrows are available doesn’t mean you have to use them where they aren’t the right tool.