EDIT: i have updated it to use for of
but still have same error of unable to access handleEventState
I am trying to subscribe and bind pusher channels/events dynamically. On ComponentDidMount, i call an API to grab available channels and their events, And on receiving the data, I subscribe and bind to those events. However the callback function on bind of events, through error of
Cannot read property 'handleEventState' of undefined
Can anyone see what's wrong here.
Constructor:
constructor(props, context) {
super(props, context);
this.state = {
site: null,
iframe_url: null,
channel_data: null,
};
this.bindData = this.bindData.bind(this);
this.handleEventState = this.handleEventState.bind(this);
}
ComponentDidMount:
axios.get(url)
.then(response => {
const data = response.data;
this.setState({channel_data: data});
this.bindData();
})
.catch((error) => {
console.log("error",error)
});
bindData:
var pusher = new Pusher('8a8708c8544df5252f22', {
cluster: 'eu',
encrypted: true
});
// get the data from mapping api
var data = this.state.channel_data.channels;
// loop each channel and subscribe to each of its event
for(let channels of data) {
var channel = pusher.subscribe(channels.channel_name);
var events = channels.events;
// Inner loop to bind the pusher event with the channel
for(let event of events ) {
channel.bind(event, this.handleEventState);
}
}