0

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);
        }
      }
Tariq G
  • 346
  • 2
  • 9
  • The functions you pass to `forEach` are not bound. Better use simple `for of` loops, or pass arrow functions. – Bergi Jul 11 '17 at 08:24
  • i have replaced forEach with `for of` but it doesn't resolve the issue. – Tariq G Jul 11 '17 at 09:29
  • Are you still getting the error in the same place? Is `this.state.channel_data.channels` working (resulting in the expected value)? From where are you calling any of these methods? Is this the exact code you are using? – Bergi Jul 11 '17 at 09:47
  • yes, same error. `this.state.channel_data.channels` working and it also subscribe to available channel. issue comes on binding to `this.handleEventState` with error `Cannot read property 'handleEventState' of undefined` And yes, this is the exact code i am using – Tariq G Jul 11 '17 at 09:58
  • Sorry. Its not giving undefined error anymore. There are other error. I will look into those. thanx. Much appreciated – Tariq G Jul 11 '17 at 10:03

0 Answers0