I want to make functions on this service asynchronous, the last function should wait to a response of the one above it and so on. I am getting 'property subscribe does not exist on type void' on the second line of createPlaylist() and I don't know why.
Imports:
import { Injectable } from '@angular/core';
import { Http, Headers, Response, URLSearchParams } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/of';
Code:
getUserId(token:string = localStorage.getItem('myToken')) {
const url = 'https://api.spotify.com/v1/me';
const headers = new Headers();
headers.append('Authorization', 'Bearer ' + token);
this.http.get(url, {headers : headers}).map((res: Response) => res.json())
.subscribe(result => {
this.currentUser = result.id;
console.log(this.currentUser);
return Observable.of(result);
});
}
createPlaylist(token:string = localStorage.getItem('myToken')) {
this.getUserId().subscribe(user => {
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(result => {
console.log(result.json().name);
console.log(result.status);
this.playlistID = result.json().id;
});
});
}
addSongs(token:string = localStorage.getItem('myToken')) {
this.createPlaylist();
const url = `https://api.spotify.com/v1/users/${this.currentUser}/playlists/${this.playlistID}/tracks`;
const headers = new Headers();
headers.append('Authorization', 'Bearer ' + token);
headers.append('Content-Type', 'application/json');
const body = {'uris': ['spotify:track:4iV5W9uYEdYUVa79Axb7Rh',
'spotify:track:1301WleyT98MSxVHPZCA6M']};
this.http.post(url, body, { headers } )
.subscribe(result => {
console.log(result.status);
});
}