Q:

Transferring data from subscribe() to variable in Angular 6

In the below function, console.log(data) is returning my payload, but I am getting "undefined" in console.log(this.finalData). What is the best practice for storing the subscribed data to my this.finalData variable?
component.ts
getFavorites(){
  this.jobService.getFavoritesFromDB().subscribe((data) =>  { 
   console.log(data)
   this.finalData = data
  });
  console.log(this.finalData)

 }

service.ts
getFavoritesFromDB(){
    return this.http.get("http://localhost:3000/post");
}

A:

You need to place the console.log inside the subscribe, since the response from the api comes asynchronously
getFavorites(){
  this.jobService.getFavoritesFromDB().subscribe((data) =>  { 
   console.log(data)
   this.finalData = data;
   console.log(this.finalData)
  });
 }

