undefined is not an object (evaluating 'this.http.get')

Issue

Can somebody PLEASE tell me what I’m doing wrong here :

I’m sorry, I feel like I’m losing my mind for 30 minutes.

getChartData() {
  this.http
    .get('http://localhost:3001/transactions/' + sessionStorage.getItem('id'))
    .toPromise()
    .then((data: any) => {
      this.data = data;
    });
}
<button (click)="getChartData()">click</button>

Error :

TypeError: undefined is not an object (evaluating 'this.http.get')

Solution

You have to return something from your function. With your current code example, If you hover your mouse over the function name, you’ll see it says void.

Also, I recommend using observables rather than promises, Angular uses heavily

getChartData() {
   // return the HTTP response
   return this.http.get( 
     'http://localhost:3001/transactions/' 
       + sessionStorage.getItem('id'))
}

Then simply, subscribe to it to get the response back

someFunction() {
   this.service.getChartData().subscribe((response) => console.log(response))
}

Answered By – Andres2142

Answer Checked By – Senaida (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.