spotify / apps-tutorial

A Spotify App that contains working examples of the use of Spotify Apps API
https://developer.spotify.com/technologies/apps/
627 stars 146 forks source link

Get starred tracks for current user #31

Closed 0m15 closed 10 years ago

0m15 commented 10 years ago

Based on what your documentation is stating here https://developer.spotify.com/docs/apps/api/1.0/api-library.html

require(['$api/library#Library'], function(Library) {
      var library = Library.forCurrentUser();
      library.starred.snapshot(0, 1).done(function(snapshot) {
            snapshot.get(0).load('name').done(function(track) {
                console.log('You have starred ' + track.name);
            });
      });
});

this snippet of code is throwing an error, since you're trying to call the snapshot method on library.starred which is a type of Playlist, which inherits from Models type and not the Collection one, so the snapshot method wouldn't be available at all.

Instead, you first have to load the correct property of the model (in this case, tracks, which inherits from the Collection type), and then call the snapshot method:

library.starred.load('tracks').done(function(playlist) { 
    playlist.snapshot(0,50).done(function(snapshot) {
       // you can now access starred tracks
       console.log(snapshot.get(0)) 
   }) 
})

This was the only way I found to get starred tracks for the current user. Is your doc correct?

Tnx.

PS: Let me know if you need further code/examples

thelinmichael commented 10 years ago

You're absolutely correct @zimok, we'll take care of this as soon as possible. Thanks for the heads up!

0m15 commented 10 years ago

You're welcome :) hope to be helpful