简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM