Is there a way to transfer a user's data to their profile? I have successfully retrieved the complete user information from the API and displayed it on the user's page. However, I encounter an error when attempting to display the same data on the user's profile page:
TypeError: Cannot read property 'username' of null
The routing seems to be functioning correctly. However, I am unable to access the user information that I intend to store in information
. I believe the following line should store the user's data:
this.userService.getUserDetails(id).subscribe(result => {
this.information = result;
});
How can this issue be resolved? Here is the breakdown of my code:
user.service
// Retrieve a list of users
getList(): Observable<any> {
return this.http.get(`${this.url}/users`);
}
// Retrieve a user's profile
getUserDetails(id): Observable<any> {
return this.http.get(`${this.url}/users/?i=${id}`); // Why include ?i
}
user.page.ts
getAllFriends() {
this.friendsList = this.userService.getList()
.pipe(map(response => response.results));
}
profile.page.ts
information = null;
...
ngOnInit() {
// Retrieve the ID from the URL parameter
let id = this.activatedRoute.snapshot.paramMap.get('id');
// Fetch the user information from the API
this.userService.getUserDetails(id).subscribe(result => {
this.information = result;
});
}
profile.page.html
<ion-label text-wrap>
<h6 class="subinfo">{{information.username}}, 22, 3589</h6>
</ion-label>