![](/img/trans.png)
[英]Firebase data returning an UNDEFINED value even though it has content
[英]Object showing as undefined in html even though it has data in the service
我对Angular并不陌生,我很轻松地遵循了《英雄之旅》教程,并使整个应用程序正常工作。 我现在已经开始一个新项目,该项目只是从Strava API获取数据以显示它。
到目前为止,我已经设法获取了一系列活动并将其显示出来,它们以对象数组的形式出现,并且我只是与下面的第一张图片做相同的事情,只是使用了不同的URL。
当我尝试招募运动员时,API用一个JSON对象响应,我可以通过在getDataFromURL
函数内部输出response.json()
来看到。
当我尝试使用{{athlete.id}}
输出到页面上时,我只是得到了Cannot read property 'id' of undefined
错误。 我已经尝试了一些改变以达到这个阶段,但是却无法正常工作。 任何帮助,将不胜感激。
//getAthlete and getDataFrom URL from the service
/* Recieve data from a given URL over jsonp */
private getDataFromURL(url: string): Promise<any> {
return this.jsonp.get(url)
.toPromise()
.then(response => response.json())
.catch(this.handleError)
}
/* Get details of a single athelete */
getAthlete(): Promise<any> {
let url = 'https://www.strava.com/api/v3/athlete?per_page=1&access_token=' + this.accessToken + '&callback=JSONP_CALLBACK';
return this.getDataFromURL(url)
}
//getAthlete from the component
athlete: Object
getAthlete(): void {
this.stravaService.getAthlete().then(athlete => this.athlete = athlete)
}
ngOnInit(): void {
this.getAthlete()
}
//Here is the html code to try and put athelete.id on the page
<span class="id">{{athlete.firstname}}</span>
采用
{{athlete?.id}}
为了让代码null
的异步加载的数据-保险箱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.