简体   繁体   中英

How to get all artists of a user (Spotify Api 1.x)?

I'm trying to get a list of Artists for a user. It was very easy in the 0.x Api, but now i can't do it.

require([
        '$api/models',
        '$api/location#Location',
        '$api/library#Library',
        '$api/search#Search',
        '$api/toplists#Toplist',
        '$views/buttons',
        '$views/list#List',
        '$views/image#Image'
        ], function(models, Location, Library, Search, Toplist, buttons, List, Image) {

    var librarys = Library.forCurrentUser();
    console.log("LIBRARY", librarys);
      librarys.load("artists").done(
          function(artists) {
              console.log(artists);

              artists.load('owner').done(function(snapshot) {
                  console.log("SNAP",snapshot);

              });
          });
});

I get a response of "Library.forCurrentUser();" but everything i try next fails. There is no error or something, but either "librarys.load("artists")" nor "librarys.snapshot()" works.

By loading "artists" from the library, you're actually not getting artists back in the callback function, but a library that has loaded the artists. Since you almost never need to have everything loaded, it's better for performance if you specify what you want to have loaded. If you wanted to load tracks as well as artists, simply use "artists", "tracks" as load parameter.

If you want to see the Artists in the current user's library, you can do like so:

require(['$api/library#Library'], function(Library) {
    Library.forCurrentUser().load("artists").done(function(library) {
        library.artists.snapshot().done(function(snapshot) {
             for (var i = 0; i < snapshot.length; i++) {       
                 console.log(snapshot.get(i).name); 
             }
        });
    });
});

Hope this helps.

Edit: This is working for the Spotify Apps API version 1.25.1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM