Reputation: 57
I am stuck on an addListener event. On receiving a message I want to call then call a function in my code but get the function not found error.
ERROR TypeError: this.plotBus is not a function
A very simple example.
this.pubnub.publish({
channel: 'test',
message:["hello"]
})
this.pubnub.addListener({
message: function(msg) {
console.log(msg);
this.plotBus(msg)
}
})
this.pubnub.subscribe({
channels: ['test'],
triggerEvents: ['message']
});
plotBus(bus){
console.log("Plotting Bus with received data")
}
Upvotes: 3
Views: 262
Reputation: 16441
The 'this' in traditional function does not work as you expect. A smart alternative to this issue is to use arrow function instead.
this.pubnub.addListener({
message: msg=> {
console.log(msg);
this.plotBus(msg)
}
})
Upvotes: 3