簡體   English   中英

undefined 不是對象(評估 'this.http.get')

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

有人可以告訴我我在這里做錯了什么:

對不起,我覺得我瘋了 30 分鍾。

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

錯誤 :

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

你必須從你的函數中返回一些東西。 對於您當前的代碼示例,如果您將鼠標懸停在函數名稱上,您會看到它顯示為void

另外,我建議使用observables而不是promises ,Angular 大量使用

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

然后簡單地訂閱它以獲取響應

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM