Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

289
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!