Is there a way to retrieve data from an api and store it in a variable within an angular component? Specifically, I am attempting to assign the data received in the subscribe
function to the loggedUser
variable and then call a separate function within the subscribe block to navigate to another component using this object. However, I encountered an error: The requested path contains undefined segment at index 1. I also want to ensure that this object is accessible outside of the subscribe method as well. How can I achieve this?
logIn() {
this.portfolioAppService.logIn(this.loggingUser).subscribe((data) => {
this.loggedUser = data;
console.log(this.loggedUser);
console.log(data);
this.navigateToProfile(this.loggedUser.Id);
});
}
navigateToProfile(id: number) {
this.router.navigate(['/profile', id]);
}