简体   繁体   中英

Spotify create playlist and add tracks

I am trying to add a "save as playlist" button to my app API 1.0.

Can someone help me figure out what is wrong here?

var player_obj = models.Album.fromURI('spotify:album:7o7UHh5PfO1kY4YoxqrwN7');
player_obj.load('tracks').done(function(a){
    var saved_playlist = new models.Playlist.create(player_obj.name);
    console.log(saved_playlist);
    a.tracks.snapshot().done(function(snapshot){
        for (var i = 0, l = snapshot.length; i < l; i++) {
            var track = snapshot.get(i);
            saved_playlist.add(track);
        }
    });
});

Get the error: Uncaught TypeError: Object # has no method 'add'

The playlist gets created, but it is empty.

When the player_obj is a playlist, I get the same error, but the playlist gets created partially with "Loading..." where the name would be in the left side.

var player_obj = models.Playlist.fromURI('spotify:user:diannallm:playlist:7A5y9BA7dxQfOdEoN8igbY');

Finally figured this out.

player_obj.load('tracks').done(function(po){
    models.Playlist.create(player_obj.name).done(function(new_playlist) {
        new_playlist.load('tracks').done(function(new_playlist_tracks) {
            po.tracks.snapshot().done(function(tracksnapshot){
                new_playlist_tracks.tracks.add(tracksnapshot.toArray());
                /*
                //This also works, but is less efficient and tracks may be entered out of order
                for (var i=0; i<tracksnapshot.length; i++){
                    var track = tracksnapshot.get(i);
                    new_playlist_tracks.tracks.add(track);
                }
                */
            });
        });
    });
});

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