I'm using the spotify API to dump all of a user's playlists, and the track names contained. I figured out how to request that info and am trying to store it in a hash where an array of tracks can be found by playlist name (hash[playlistName] = [track1, track2, etc]
). When I console.log this, everything looks fine.
But when I try and use a handle bars helper to iterate through this and display everything, JavaScript thinks the object is empty. Here is some code:
//helper that will display eventually, but right now logs "undefined"
Handlebars.registerHelper('read_hash', function(hash){
console.log(hash.length); //undefined
console.log(Object.keys(hash); //undefined
console.log(hash); //looks fine.. results in screenshot..
});
//ajax request that successfully get's all of the playlist data I want
$.ajax({
url: 'https://api.spotify.com/v1/me/playlists',
headers: {
'Authorization': 'Bearer ' + access_token
},
success: function(response) {
//make a new hash and put in template
var processed = 0;
response.items.forEach(function(e){
var playlistName = e.name;
//get tracks by nesting a request lol
$.ajax({
url: e.tracks.href,
headers: {
'Authorization': 'Bearer ' + access_token
},
success: function(responseTwo) { //returns track objects
playlists[playlistName] = responseTwo.items;
processed++;
}
}).done(function(){
if(processed >= response.items.length)
{
playlistPlaceholder.innerHTML = playlistTemplate({Playlists: playlists}); //get playlists
}
});
});
//handle bars template with call to read_hash
<script id="playlist-template" type="text/x-handlebars-template">
<h1>playlists</h1>
<table>
<tr>
<th>playlist</th>
<th>tracks</th>
</tr>
{{read_hash Playlists}}
</table>
</script>
Also, when I console log JSON.stringify(hash) from read_hash I get results I want so... maybe something is working?