SOLVED: Cannot read property 'addMessage' of undefined


#1

Hi all,
I’m using socket.io to send/receive messages.into chat page very similar to kitchensink messages page.
I’ve error Cannot read property ‘addMessage’ of undefined when receiving messages but only the second time I open the messages page.
Framework7Component object seems empty when entering the event callback the second time.
Framework7 version is 4.2.0

This is the code in the initPage:

app.on('chat_message', (data) => {
  console.log(JSON.stringify(data));
  self.messages.addMessage({
    text: data.message,
    type: 'received',
    name: data.name,
    avatar: data.avatar
  });
});

First time I enter the page messages receiving is ok.
Then I go back with navbar back button.
Then re-enter the page and this time messages receiving isn’t working anymore with the error above.


#2

Solved with app.off:
pageBeforeRemove: function (e, page) {
var self = this;
if (self.messagebar) {
self.messagebar.destroy();
}
app.off(‘chat_message’);
},