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

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 1:[1]

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))
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andres2142