0

I'm Implementing small chat application using socket.io gladly it's working fine. But my concern is when I got a new chat I need to assign it to string array to display in listview.

I have simply define array as "messages: string[] = [];" and pushed sample string on page load and it's working fine.But when I got new message from a socket this.socket.on('newmessage', function (data) method will fire and can read new message.those all things working correctly.

But When I push new string into my "messages: string[] = [];" array. I'm getting 'Cannot read property 'push' of undefined' error.

import { Component, OnInit} from '@angular/core';
import * as io from 'socket.io-client';

@Component({
  selector: 'app-chatbox',
  templateUrl: './chatbox.component.html',
  styleUrls: ['./chatbox.component.css'],

})

export class ChatboxComponent implements OnInit {
  socket;
  messages: string[] = [];
  
  constructor() { this.socket = io.connect('http://localhost:8000'); }

  ngOnInit() {
    this.initializeChatServer();
  }

  initializeChatServer() {
  
    this.messages.push( 'test 55');//This line works

    this.socket.on('newmessage', function (data) {
      console.log('message -> ' + data.nick + '>' + data.msg);     
      this.messages.push(data.msg); //Cannot read property 'push' of undefined
    }); 
    
  }

}
Isanka Thalagala
  • 1,625
  • 3
  • 16
  • 32
  • 2
    Possible duplicate of [How to access the correct \`this\` inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – JB Nizet Aug 28 '18 at 06:31
  • 2
    Here we go again. Use an arrow function, not an anonymous function: `this.socket.on('newmessage', data => { ... })` – JB Nizet Aug 28 '18 at 06:32
  • same comment, solve it using a fat arrow or .bind – Flavien Volken Aug 28 '18 at 07:55

3 Answers3

1

I think it's because of synchronize call.I think it's not the standard way but it worked after I change like this..

 initializeChatServer() {

    this.messages.push( 'test 55');
    var self = this;//assgin this to var variable
    this.socket.on('newmessage', data => {
      console.log('message -> ' + data.nick + '>' + data.msg);     
      self.messages.push(data.msg); 
    });

  }
Isanka Thalagala
  • 1,625
  • 3
  • 16
  • 32
0

this.messages.push(data.msg); //Cannot read property 'push' of undefined

Because you have the wrong this. Arrow functions will fix that e.g. Change this.socket.on('newmessage', function (data) { to this.socket.on('newmessage', (data) => {

basarat
  • 261,912
  • 58
  • 460
  • 511
0
import { Component, OnInit} from '@angular/core';
import * as io from 'socket.io-client';

@Component({
  selector: 'app-chatbox',
  templateUrl: './chatbox.component.html',
  styleUrls: ['./chatbox.component.css'],
})

export class ChatboxComponent implements OnInit {
  socket;
  messages: string[] = [];

  constructor() { this.socket = io.connect('http://localhost:8000'); }

  ngOnInit() {
    this.initializeChatServer();
  }

  initializeChatServer() {

    this.messages.push( 'test 55');//This line works

    this.socket.on('newmessage', data => {
      console.log('message -> ' + data.nick + '>' + data.msg);     
      this.messages.push(data.msg); //Cannot read property 'push' of undefined
    });

  }

}
Charly
  • 166
  • 14