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() {
    .get('http://localhost:3001/transactions/' + sessionStorage.getItem('id'))
    .then((data: any) => {
      this.data = data;
<button (click)="getChartData()">click</button>

Error :

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


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( 
       + 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.