如何在nodejs中从Spotify API获取每一页播放列表
How to get every page of playlists from Spotify API in nodejs
我正在尝试从 spotify API 获取用户的所有播放列表并将它们放入选择列表中。我一次只能访问 20 个播放列表,所以我创建了一个 while 循环,它使用下一页 属性 遍历播放列表的每一页,但由于某种原因,它导致了无限循环。我猜问题出在 uri = playlist.next 行中,如下所示。任何帮助将不胜感激,这是代码:
//Request the user's information...
$.ajax({
url: 'https://api.spotify.com/v1/me',
headers: {
'Authorization': 'Bearer ' + access_token
},
//... Create a list of the 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 to the menu
$.ajax({
url: nextURL,
headers: {
'Authorization': 'Bearer ' + access_token
},
success: function(playlists) {
console.log(nextURL);
var i = 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 the logged in div.
$('#login').hide();
$('#loggedin').show();
}
});
答案归功于JMPerez:
I think the problem is that you are considering the ajax requests to be blocking, when they are asynchronous. Instead of having he request in you infinite loop, make the request to the next chunk in the success callback of your previous request. Create a function that makes a request accepting an offset (or the 'next' URLs and then inside that function, when the request has succeeded, call itself with the URL of the next chunk.
更新后的代码已添加到上述问题中。
我正在尝试从 spotify API 获取用户的所有播放列表并将它们放入选择列表中。我一次只能访问 20 个播放列表,所以我创建了一个 while 循环,它使用下一页 属性 遍历播放列表的每一页,但由于某种原因,它导致了无限循环。我猜问题出在 uri = playlist.next 行中,如下所示。任何帮助将不胜感激,这是代码:
//Request the user's information...
$.ajax({
url: 'https://api.spotify.com/v1/me',
headers: {
'Authorization': 'Bearer ' + access_token
},
//... Create a list of the 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 to the menu
$.ajax({
url: nextURL,
headers: {
'Authorization': 'Bearer ' + access_token
},
success: function(playlists) {
console.log(nextURL);
var i = 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 the logged in div.
$('#login').hide();
$('#loggedin').show();
}
});
答案归功于JMPerez:
I think the problem is that you are considering the ajax requests to be blocking, when they are asynchronous. Instead of having he request in you infinite loop, make the request to the next chunk in the success callback of your previous request. Create a function that makes a request accepting an offset (or the 'next' URLs and then inside that function, when the request has succeeded, call itself with the URL of the next chunk.
更新后的代码已添加到上述问题中。