Here is a snippet from my express.js file:
function playlistTracks(req, res) {
var tracks = [];
var genres = ['hi'];
var playlistOption = {
url: 'https://api.spotify.com/v1/users/' + req.params.user + '/playlists/'+ req.params.id + '/tracks',
headers: { 'Authorization': 'Bearer ' + access_token },
json: true
};
rp(playlistOption)
.then(function (body) {
tracks = body;
return tracks;
})
.then(function (tracks) {
getGenre(tracks);
res.render('tracks', {
data: tracks
});
})
.catch(function (err) {
console.log('couldnt get tracks' , err);
throw err;
});
function getGenre(tracks) {
tracks.items.forEach(function(e){
var reqGenre = {
url: 'https://api.spotify.com/v1/artists/' + e.track.album.artists[0].id,
json: true
};
rp(reqGenre)
.then(function(body) {
genres.push(body.genres)
})
.catch(function(err){
console.log('couldnt get genres' , err);
throw err
});
});
io.emit('playlists', {genres: genres});
console.log(genres) // <---empty
}
}
the "getGenre" function is where I have the most trouble with. I want to know how I can update the "genres" array with that function. I've tried a couple of solutions but can't seem to get my head around the async nature of a request.
I've looked at this and other solutions already, but can't figure out how to apply them to my code.
I'm using request-promise to get the api request.