根据函数内的返回值创建变量,以在该函数外使用

时间:2018-11-22 22:35:00

标签: javascript ajax function callback spotify

我正在创建一个包含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);  
}

0 个答案:

没有答案