Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn Top Track Spotify API
    primarykey
    data
    text
    <p>I am trying to create a helper file that will return the top track(s) of any artist's related artists. All I want to do use 1 artist URI to surface their related artists' name, popularity, and top track. And I want to separate out the top track functionality into a separate file that I can call whenever.</p> <p>But, I can't seem to figure out how to properly return the top track of the related artists.</p> <p>In my "get-toptrack.js" file:</p> <pre><code> require([ '$api/models', '$api/toplists#Toplist' ], function(models, Toplist) { 'use strict'; var doGetTopTrack = function(uri, num) { var artistURI = uri; var artist = models.Artist.fromURI(artistURI); var artistTopList = Toplist.forArtist(artist); artistTopList.tracks.snapshot().done(function(snapshot){ snapshot.loadAll('name').done(function(tracks) { var i, num_toptracks; num_toptracks = num; for(i = 0; i &lt; num_toptracks; i++){ console.log("top track: " + tracks[i].name); // WHERE DO I RETURN THE TOP TRACKS?? } }); }); }; exports.doGetTopTrack = doGetTopTrack; }); </code></pre> <p>In my "artist.js" file":</p> <pre><code>require([ '$api/models', 'scripts/get-toptrack' ], function(models, getTopTrack) { 'use strict'; var showRelated = function() { var artist_properties = ['name', 'popularity','related', 'uri']; models.Artist .fromURI('spotify:artist:11FY888Qctoy6YueCpFkXT') .load(artist_properties) .done(function(artist){ artist.related.snapshot().done(function(snapshot){ snapshot.loadAll('name').done(function(artists) { for(var i = 0; i &lt; artists.length; i++){ var u, p, n, t, listItem; // store artist details p = artists[i].popularity; n = artists[i].name; u = artists[i].uri; // am I missing something here? t = getTopTrack.doGetTopTrack(u, 1); listItem = document.createElement("li"); listItem.innerHTML = "&lt;strong&gt;Name&lt;/strong&gt;: " + n.decodeForText() + " | &lt;strong&gt;Popularity: &lt;/strong&gt; " + p + " | &lt;strong&gt;Top Track: &lt;/strong&gt;" + t; // undefined name $('#artistsContainer').append(listItem); } }); }); }); }; exports.showArtists = showArtists; }); </code></pre> <p>And in the 'main.js' file, I call the artists function to begin.</p> <pre><code>require([ '$api/models', 'scripts/artist' ], function(models, initArtist) { 'use strict'; initArtist.showRelated(); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload