javascript - How to get every page of playlists from Spotify API in nodejs -
i'm trying of user's playlists spotify api , put them in selecting list. can access 20 playlists @ time created while loop uses next-page property go through every page of playlists, reason results in infinite loop. i'm guessing issue in uri = playlist.next line, highlighted below. appreciated, here's code:
//request user's information... $.ajax({ url: 'https://api.spotify.com/v1/me', headers: { 'authorization': 'bearer ' + access_token }, //... create list of user's playlists success: function(user) { var playlisturl = 'https://api.spotify.com/v1/users/' + user.id + '/playlists'; appendplaylists(playlisturl); function appendplaylists(nexturl) { if (nexturl == null) { return; } //append playlists menu $.ajax({ url: nexturl, headers: { 'authorization': 'bearer ' + access_token }, success: function(playlists) { console.log(nexturl); var = 0; while (playlists.items[i] != null) { console.log(playlists.items[i].name); $('.all-playlists').append('<option value="' + playlists.items[i].href + '">' + playlists.items[i].name + '</option>') i++; } appendplaylists(playlists.next); } }); } //load logged in div. $('#login').hide(); $('#loggedin').show(); } });
credit jmperez answer:
i think problem considering ajax requests blocking, when asynchronous. instead of having request in infinite loop, make request next chunk in success callback of previous request. create function makes request accepting offset (or 'next' urls , inside function, when request has succeeded, call url of next chunk.
the updated code has been added question above.
Comments
Post a Comment