I have a typescript object like this
const playlist: { tracks: Array<Track> } = { tracks: new Array<Track>() };
And this is Track interface
interface Track {
title?: string;
album?: string;
artists?: string;
duration?: string;
explicit?: boolean;
url?: string;
}
I'm trying to get data from Spotify web API to my playlist
object like this
for (let i = 1; i <= iterationCount; i++) {
const endpointX = `${BASE_API_URL}playlists/${playlistId}/tracks?limit=${PLAYLIST_ITEM_LIMIT}&offset=${i}&fields=${fieldString}`;
const subscription: Subscription = this.http.get<WritableTrackList>(endpointX).subscribe(data => {
for (const item of data.items) {
const track: Track = this.helperService.trackApiObject2TrackObject(item.track);
this.playlist.tracks.push(track);
}
});
}
When I try to print playlist
object in the javascript console with console.log(this.playlist);
, it's working.
Buth I try to print playlist.tracks
with console.log(this.playlist.tracks);
, it's print an empty array
Why can't I access the tracks
property of the playlist
object? How can i access this property? Any solution?