0

I decided to try something new this weekend and started a NodeJS project, which should give me a random new Spotify song. Therefore I'm using the node-spotify-api npm module. It seems like i managed to authenticate to the Spotify API but I don't know how to access the response correctly. Here is my code so far:

// Get random Song
app.get('/getrandomsong', async (req, res) => {
    console.log('Starting to search for a random song');
    // Authenticate to spotify api
    const spotify = new Spotify({
        id: process.env.SPOTIFY_ID,
        secret: process.env.SPOTIFY_SECRET
    });

    // Make the actual request 
    spotify
        .request('https://api.spotify.com/v1/browse/new-releases?limit=1')
        .then(function(data) {
        console.log(data);
    }) .catch(function(err) {
        console.log('Error occured: ' + err);
    });
});

The console log shows me the following response. How can I access and use the information in [Object]?

Starting to search for a random song
{
  albums: {
    href: 'https://api.spotify.com/v1/browse/new-releases?offset=0&limit=1',
    items: [ [Object] ],
    limit: 1,
    next: 'https://api.spotify.com/v1/browse/new-releases?offset=1&limit=1',
    offset: 0,
    previous: null,
    total: 100
  }
}
1
  • data.albums.items Commented Sep 20, 2020 at 9:41

1 Answer 1

1

You can access it by using data.albums.items

You can also check the content of the items array to see what your Object is, by doing the following

data.albums.items.forEach(arr => {
  console.log(arr)
})
Sign up to request clarification or add additional context in comments.

2 Comments

Do i have to install something? If i log data.albums it shows more information but data.albums.items is not defined
Nevermind. I managed to get the values with data['albums']['items']

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.