Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

276
Vistas
Cannot read property 'emit' of undefined typescript

im building a simple chat app with socket io angular 2(typescript) and nodejs as a backend server i have this function which works perfectly when i click the connect button

connectUser(name){
  
  	 
  	if (name == null || name == ""){
  	return 	console.log('you must enter a valid username');
  	
  	}
  	this.username = name;

  	this.socket.emit('add-user',name);
  	//load connected users in the user-window section
  	this.socket.emit('request-users',{data:'request users success'});
  	var connecctedUsersListener$ = Observable.fromEvent(this.socket,'users');
  	connecctedUsersListener$.subscribe((observer:any)=>{
  		this.users = observer;
 	},(err)=>{
  		if(err) throw err;
  	},()=>{
  		console.log('complet');
  	});
  
  //load messages and keep track of Them 
  this.socket.emit('request-messages',{data:'request Messages success'});
  var messageListener$ = Observable.fromEvent(this.socket,'message');
  messageListener$.subscribe((observer:any) =>{
  	this.messages = observer;
  	
  })

  }

however i try to use bootbox.prompt whenever the user tries to connect so i wrap all the code inside this simple function

addUser(){
  bootbox.prompt('what is your name ',function(name){
  connectUser(name); // this is the function in the first code snippet 	

  	  });
  }

i get this wierd error Cannot read property 'emit' of undefined how can i fix that and what is the problem here ?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

When you are calling connectUser from addUser you are doing it from another function (apparently a callback).

Javascript binds 'this' to that wrapper function you are using, and that function doesn't have a 'socket' member.

When you call the function connectUser from the connect button directly, you are probably calling it inline from the object, thus, 'this' is bound to the object that does contain the 'socket' member.

Review the following snippet to have a look on how the "function() { }" wrapper is affecting you:

class Whatever {
   socket: { emit: string }

   constructor()
   {
      this.socket = { emit: 'works' };
   }

   doSomething()
   {
      console.warn(this.socket.emit);
   }

   works()
   {
      this.doSomething();
      (() => this.doSomething())();
   }

   doesntWork()
   {
      (function () { this.doSomething() })();
   }
}

let obj = new Whatever();
obj.works();
obj.doesntWork();

Basically you can probably just change this:

function(name) { connectUser(name); }

For this

(name) => connectUser(name) 

and it should work.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda