1

I'm new to angular and i'm trying to figure out how can I save in a local variable the response of http.get(url)

Here is my code :

export class AppComponent {

  private url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&api_key=MY_KEY&format=json&artist=The+Weeknd' ;
  public data;

  constructor(private http: HttpClient) { 
    this.http.get(this.url).subscribe(response => this.data = response);
    console.log(this.data); // -> The result is undefined...
  }

}

At first, I tried this.http.get(this.url).subscribe(response => console.log(response)); and that was working has expected, however an assignation doesn't work.

Thanks a lot !

6
  • log this.data in subscribe, it's undefined because you log the data before the api response has arrived. Commented Aug 31, 2018 at 18:06
  • The http request is an asynchronous operations. The data will be saved, after the request has gotten a response. Commented Aug 31, 2018 at 18:06
  • This command is asynchronous ‘this.http.get(this.url).subscribe(response => this.data = response);‘ Like setTimeout. You trying to console data before subscribe event fire Commented Aug 31, 2018 at 18:06
  • Is there another method I can use to achieve the same thing synchronously ? Commented Aug 31, 2018 at 18:10
  • what do you mean synchronously? fetching the data from API takes time so if you want to achieve this data you should wait and do it asynchronous. Commented Aug 31, 2018 at 18:17

2 Answers 2

2

Your code is exactly correct. The reason the console.log is not showing the response value is because it is running BEFORE the response is processed. Once the HTTP request has been started, JavaScript continues executing the current function.

If you want to log the response, you need to do so inside the response handler

export class AppComponent {

  private url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&api_key=MY_KEY&format=json&artist=The+Weeknd' ;
  public data;

  constructor(private http: HttpClient) { 
    this.http.get(this.url).subscribe(response => {
        this.data = response;
        console.log(this.data);
    });
  }    
}
Sign up to request clarification or add additional context in comments.

6 Comments

Ok I understand that subscribe() is asynchronous, is there another method I can use to achieve this synchronously ?
@obrassard I don't know. It's possible there is something with async/await that will be close, but I recommend you don't bother. Web requests by their nature are asynchronous and you should work on being comfortable with them. You mention you're new, and I understand how annoying and frustrating that learning is - but it will greatly help you in the long run.
In the response handler, how an i access each property of the json object received ? This is the result in the console : {name: "The Weeknd", mbid: "c8b03190-306c-4120-bb0b-6f2ebfc06ea9", url: ...} but when i try to get response.name for instance it's undefined
@obrassard Without running the code myself, it's hard to guess at the reason. However, I think it may be an issue with the typing, so I would look into angular.io/guide/http#type-checking-the-response. Since you have not told Angular the structure of the response (by creating a class for it), it may not know how to parse the response into an object and is leaving as a string - thus the output to the console as a string and .name is undefined
If i create a class, did I have to include every property and array of the response json object ? Can I add only the one that matters for my project ?
|
0

You are doing async HTTP call. so you need to add console.log inside subscribe.

export class AppComponent {

  private url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&api_key=MY_KEY&format=json&artist=The+Weeknd' ;
  public data;

  constructor(private http: HttpClient) { 
    this.http.get(this.url).subscribe(response => {
       this.data = response;
       console.log(this.data);
    });
  }

1 Comment

I see, and in the response handler, do you know how an I can access each property of the json object received ? This is the result in the console : {name: "The Weeknd", mbid: "c8b03190-306c-4120-bb0b-6f2ebfc06ea9", url: ...} but when i try to get response.name for instance it's undefined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.