我正在从Spring编写的REST API中获取数据。我正在使用HttpClient#get使用REST API,并且确实收到了正确的数据。问题是我一直在控制台中收到此错误: 错误TypeError:无法读取未定义的属性'getUsername'
我有一个userService,它封装了从REST API检索用户数据:
public getUserDetails(username: string): Observable<any>
{
let url: string = `http://localhost:8080/api/${username}`;
return this.http.get(url);
}
然后在ngOnInit方法的UserComponent中调用此方法,如下所示:
private getUserDetails(): void
{
this.userService.getUserDetails().subscribe((response) => {
this.user = new User();
this.user.setProfilePicture(response.profile);
this.user.setFollowers(response.followers);
this.user.setFollowing(response.following);
this.user.setFullName(response.fullName);
this.user.setUsername(response.username);
}, (error) => {
console.log(error);
});
}
public ngOnInit()
{
this.getUserDetails();
}
用户的详细信息在模板中已适当更新,但我仍然看到 错误TypeError:无法读取控制台中未定义的属性'getUsername'