我正在创建一个包含Spotify Web API的应用程序。我正在尝试为返回的歌曲ID创建一个变量,但不确定如何在函数外访问它。
//Takes name of song from hard coded JSON list and searches for track in Spotify.
//Returns spotify id of track
linkToSpotify(request, response) {
const playlistId = request.params.id;
logger.debug(playlistId);
const song = request.params.songid;
var songOne;
const body = playlistStore.getPlaylist(playlistId);
body.songs.forEach(function(element) {
if (element.songId == song) {
songOne = element;
}
});
$(function myFunction() {
let query = songOne.name;
$.get('/search?' + $.param({query: query}), function(data) {
document.getElementById('results').innerHTML = data.tracks.items.map(track => {
return {trackid : track.id.spotify};
});
});
});
const viewData = {
spotifyId : trackid
};
response.render('viewsongs', viewData);
}