I'm a bit new to angular and I have a problem where I can console.log the response from a "get", but when I try to save it to a variable, the variable is always empty. Something to do with it being asynchronous I think because I see the console trying to print out the response variable before it prints out data from inside the subscribe. This is just bare bones code but I think it demonstrates my problem.
response;
constructor(private http: HttpClient) {
this.getResponse();
console.log(response);
}
getResponse() {
return this.http.get(URL).subscribe((data) => {
this.response = data;
console.log(data);
);
}
In this example, the second console log, console.log(data), prints out the appropriate response but the first one, console.log(response), is blank. What's frustrating is every single stack overflow response I've found about using http get simply console logs the response instead of showing how to properly store the response and access it later! What do I need to do to store the response from the get into a variable and then be able to work with that variable?