Thesystem32 Thesystem32 - 7 months ago 27
Javascript Question

Spotify API node

Im not entirely sure why my cmd keeps returning

undefined
when I type in
console.log(data2.body.artists.name);
.

But when I keep it at
console.log(data2.body.artists);
it will display the screen shot I included.

Anyone know whats going wrong here? I just want the artist name to appear.

spotifyApi.getArtistRelatedArtists('137W8MRPWKqSmrBGDBFSop')
.then(function(data2) {
console.log('Top Related Artists:')
console.log(data2.body.artists.name);
}, function(err) {
done(err);
});

Answer

data2.body.artists is an array of objects. So you need to get a name of the artist at some index. For example the first artists name:

data2.body.artists[0].name