How to get data from Observable in Angular? [closed]

  angular, typescript

I want to get the avatarId from user and save it in a variable, this is what I tried so far:

this.userService.get(this.currentUser.id).subscribe(user => 
    {
      this.user = user;
      this.avatarId = this.user.avatarId
    });
import { TokenStorageService } from '../../../services/auth/token-storage.service';
import { UserService } from '../../../services/user/user.service';
import { Router } from '@angular/router';
import { AvatarService} from '../../../services/avatar/avatar.service';
import { User } from '../../../models/user/user.model';

@Component({
  selector: 'app-perfil',
  templateUrl: './perfil.component.html',
  styleUrls: ['./perfil.component.css']
})
export class PerfilComponent implements OnInit {

  currentUser: any;
  user: {};
  avatarId: any;

  constructor(private avatarService: AvatarService, private userService:UserService, private tokenStorage: TokenStorageService, private irHacia:Router) { }

  ngOnInit(): void {
    this.user = new User();
    this.currentUser = this.tokenStorage.getUser();
    this.userService.get(this.currentUser.id).subscribe(user => 
    {
      this.user = user;
      this.avatarId = this.user.avatarId
    });

    if (!this.tokenStorage.getToken()) {
      this.irHacia.navigate(['/login'])
    }
    
  }


}

Source: Angular Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.