0

When a button click triggers createPlaylist() the function fails to execute asd(). I tried doing it all on the same function but I get the same. console.log(resp) never logs. Why is that?

    createPlaylist(token:string = localStorage.getItem('myToken')) {

      const url = `https://api.spotify.com/v1/users/${this.currentUser}/playlists`;
      const headers = new Headers();
      headers.append('Authorization', 'Bearer ' + token);
      headers.append('Content-Type', 'application/json');
      const body = {
        'name': 'searchDDD playlist'
      };
      this.http.post(url, body, { headers } )
      .subscribe(res => {
        console.log(res.json().name);
        console.log(res.status);
        this.playlistID = res.json().id;
        this.asd();
      });


    }

    asd(token:string = localStorage.getItem('myToken')){
      const url = `https://api.spotify.com/v1/users/${this.currentUser}/playlists/${this.playlistID}/tracks?`;
      const body = {'uris': ['spotify:track:4iV5W9uYEdYUVa79Axb7Rh',
      'spotify:track:1301WleyT98MSxVHPZCA6M']};
      const headers = new Headers();
      headers.append('Authorization', 'Bearer ' + token);
      headers.append('Content-type', 'application/json');
      this.http.post(url, body, { headers } ).map(resp => {
        console.log(resp);
    });
}
victor.ja
  • 811
  • 1
  • 7
  • 27
  • 1
    Possible duplicate of [Angular 2 http get not getting](https://stackoverflow.com/questions/41381200/angular-2-http-get-not-getting) – jonrsharpe Nov 14 '17 at 08:09

1 Answers1

1

Without subscribe() (or forEach() or some others that actually execute the observable) observables won't do anything, because they are lazy

this.http.post(url, body, { headers } ).subscribe(resp => {
    console.log(resp);
});
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567