简体   繁体   English

undefined 不是对象(评估 'this.http.get')

[英]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.对不起,我觉得我疯了 30 分钟。

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 .对于您当前的代码示例,如果您将鼠标悬停在函数名称上,您会看到它显示为void

Also, I recommend using observables rather than promises , Angular uses heavily另外,我建议使用observables而不是promises ,Angular 大量使用

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义不是对象(评估_this2.props.navigation.navigate) - undefined is not an object (evaluating _this2.props.navigation.navigate) undefined 不是对象(评估“firebase.initializeApp”) - undefined is not an object (evaluating 'firebase.initializeApp') TypeError: undefined is not an object(评估“ReactCurrentActQueue$1.isBatchingLegacy”) - TypeError: undefined is not an object (evaluating 'ReactCurrentActQueue$1.isBatchingLegacy') Undefined 不是一个对象(评估 RNRandomBytes.seed) - Undefined is not an object (evaluating RNRandomBytes.seed) undefined不是对象(评估&#39;storeDataFactory.createUser(user).then - undefined is not an object (evaluating 'storeDataFactory.createUser(user).then undefined 不是 object(评估“item.Attraction.Name”) - undefined is not an object (evaluating 'item.Attraction.Name') TypeError:未定义不是对象(评估“ this .__ reactAutoBindMap”) - TypeError: undefined is not an object (evaluating 'this.__reactAutoBindMap') 未定义不是对象(评估“ this.login()。bind”) - undefined is not an object (evaluating 'this.login().bind') Sinon FakeXMLHttpRequest TypeError:“未定义”不是对象(评估“ this.requests [0]”) - Sinon FakeXMLHttpRequest TypeError: 'undefined' is not an object (evaluating 'this.requests[0]') MERN:TypeError:未定义不是对象(正在评估“ this.props.params.id”) - MERN: TypeError: undefined is not an object (evaluating 'this.props.params.id')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM