Intermittent issue with tracks snapshot for current user top list

106 views Asked by At

I have a problem when executing this code:

require(['$api/models','$api/library#Library'], function(models,Library) {

// THIS ONLY HAPPEN FOR TOP LIST
var uri=Library.forCurrentUser().toplist.uri;
// IF YOU USE ANY OTHER PLAYLIST IT WORKS FINE
//  var uri="spotify:user:vdesabou:playlist:0xy2zExFmPzJZsY0X0bCC5";

var playlist = models.Playlist.fromURI(uri);
playlist.load('tracks').done(function() {
    console.log("loaded 1");
playlist.tracks.snapshot().done(function(snapshot) {
    console.log("snapshot length 1 " + snapshot.length);

    snapshot.loadAll('name')
       .done(function(snap_tracks) { console.log("loaded tracks length 1 " + snap_tracks.length);    })
       .fail(function() { console.log("loadAll failed"); });
    }).fail(function() { console.log("snapshot failed"); });

}).fail(function() { console.log("playlist load tracks failed"); });

});

If I execute multiple time (by reloading my application), I don't get results about 1 time out of 3

When it doesn't work:

loaded 1

When it works I get:

loaded 1
snapshot length 1 20 
loaded tracks length 1 20 

This is happening only for top list playlist, any other playlist is ok.

What could be wrong? Thanks

2

There are 2 answers

1
Michael Thelin On BEST ANSWER

Library's toplist property is already a playlist, so you don't need to create a new Playlist object from its URI. This snippet does the same as yours, but loads the toplist property and uses it to get the tracks directly instead of creating a new playlist.

require(['$api/models','$api/library#Library'], function(models,Library) {

  var library = Library.forCurrentUser();
  library.load("toplist").done(function() {
    var toplist = library.toplist;

    toplist.load('tracks').done(function() {
      console.log("loaded 1");
      toplist.tracks.snapshot().done(function(snapshot) {
        console.log("snapshot length 1 " + snapshot.length);

        snapshot.loadAll('name')
          .done(function(snap_tracks) { console.log("loaded tracks length 1 " + snap_tracks.length);    })
          .fail(function() { console.log("loadAll failed"); });

      }).fail(function() { console.log("snapshot failed"); });

    }).fail(function() { console.log("playlist load tracks failed"); });

  }).fail(function() {
    console.log("Could not load toplist.");
  });

});

i did try the snippet you provided, and similarly to Thomas I couldn't find anything wrong with it. Hope this helps though.

5
Thomas On

Either it's a localized issue (I can't repro), or due to the asynchronous nature of the functions, you can't make the same call twice on the same playlist.

I've tried your code 3 times (on Windows v0.9.6) without seeing the behavior you're seeing.

Renaming playlist1,2,3 didn't change the behavior for me, works both ways.