Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR TypeError: Cannot read property 'get' of undefined

enter image description hereERROR TypeError: Cannot read property 'get' of undefined

im new in angular i really need help and thanks in advance

constructor(private http: Http, private authService: AuthService, private router: Router) {
	this.token = localStorage.getItem('token');
	
	if(token){
		this.interval = setInterval(this.ingameee, 5000);
	}
}

ingameee() {
   this.http.get('http://url.com/api/matchmaking/keepalive?token='+ this.token)
          .subscribe(
              response => {
                  this.dataa = response;
                  this.mod = this.dataa.json().mod;
                  this.playersinqueue = this.dataa.json().playersinqueue;
                  this.region_id = this.dataa.json().region_id;
                  this.party_id = this.dataa.json().party_id;
                  },
              error => console.log(error),
          );

  }
like image 643
has van Avatar asked Jul 12 '17 05:07

has van


People also ask

How do you fix TypeError Cannot read property of undefined?

To resolve your TypeError: Cannot read properties of undefined (reading '0') , go through these steps: Ensure you are using the correct variable. Perform a simple check on your variable before using it to make sure it is not undefined. Create a default value for the variable to use if it does happen to be undefined.

Can not read the property of undefined then?

TypeError - Cannot read property 'then' of undefined is thrown when the caller is expecting a Promise to be returned and instead receives undefined . Let's consider the above examples. In Example 1, the getTaxAmount(price, taxRate) function, when called, should have returned a Promise that resolves to a value of 12 .

How do you fix TypeError Cannot read properties of null?

The "Cannot read property 'click' of null" error occurs when trying to call the click method on a null value. To solve the error, run the JS script after the DOM elements are available and make sure you only call the method on valid DOM elements.

Can not set property of undefined?

The "Cannot set properties of undefined" error occurs when setting a property on an undefined value. To solve the error, conditionally check if the value is of the expected type (object or array) or has to be initialized before setting the property on it.


1 Answers

keep in mind to use arrow function for keeping the context while using callbacks because this(the context) will change.

this.interval = setInterval(() => this.ingameee(), 5000);
like image 104
Pengyy Avatar answered Nov 15 '22 03:11

Pengyy