I am trying to store the JSON data which has been send from the backend (Spring boot application) to the front end (angular). Now everytime I want to store the data in a variable, once I try to access it outside of the subscribe, it will be stored as undefined and will cause errors, I am also working with multiple files and components and I need to pass that data over multiple files.
export class AppComponent implements OnInit {
  routes: RouteItem[] = [];
  public serverMessage = ''; // <=== this is my main variable
  constructor(private http: HttpClient, private routeService: RouteService) {
  }
  ngOnInit() {
    this.http.get('http://localhost:8080/get-json').pipe(
      first(),
      tap(result => console.log('Message received from the server: ', result)),
      map(result => this.serverMessage = (result as any).jsonMessage)
    ).subscribe(data => {
        this.routes = this.routeService.getRoutes(this.serverMessage);
      }
    );
// If I put a console.log(this.serverMessage); here, it wont recognize the data anymore.
  }
}
Can anyone give tips and advise on how to tackle this issue?
 
    