繁体   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