Search code examples
node.jsjsonspotifyconsole.log

console.log returns "Object" for some fields, instead of body data [spotify-web-api-node]


I'm using the spotify-web-api-node and when I do api calls, the returned data looks like that in the console:

Album information {
  album_type: 'album',
  artists: [
    {
      external_urls: [Object],
      href: 'https://api.spotify.com/v1/artists/2hazSY4Ef3aB9ATXW7F5w3',   
      id: '2hazSY4Ef3aB9ATXW7F5w3',
      name: 'IZAL',
      type: 'artist',
      uri: 'spotify:artist:2hazSY4Ef3aB9ATXW7F5w3'
    }
  ],

  ...

  label: 'Hook Ediciones Musicales',
  name: 'Agujeros de Gusano',
  popularity: 0,
  release_date: '2014-01-25',
  release_date_precision: 'day',
  total_tracks: 13,
  tracks: {
offset=0&limit=50',
    items: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ],
    limit: 50,
    next: null,
    offset: 0,
    previous: null,
    total: 13
  },
  type: 'album',
  uri: 'spotify:album:5U4W9E5WsYb2jUQWePT8Xm'
}

As you can see, some fields like external_urls: and (later) items: have [Object] returned, instead of the actual data. The (node.js) code for that particular example is the following:

spotifyApi.getAlbum('5U4W9E5WsYb2jUQWePT8Xm')
  .then(function(data) {
    console.log('Album information',data.body);
  }, function(err) {
    console.error(err);
});

If I change the data.body part in console.log to JSON.stringify(data.body) it is somewhat better, as it does return the data, but as a string (obviously), which not exactly... useable:

Album information {"album_type":"album","artists":[{"external_urls":{"spotify":"https://open.spotify.com/artist/2hazSY4Ef3aB9ATXW7F5w3"},"href":"https://api.spotify.com/v1/artists/2hazSY4Ef3aB9ATXW7F5w3","id":"2hazSY4Ef3aB9ATXW7F5w3","name":"IZAL","type":"artist","uri":"spotify:artist:2hazSY4Ef3aB9ATXW7F5w3"}],"available_markets":[],"copyrights":[{"text":"2013 Hook Ediciones Musicales","type":"C"},{"text":"2013 Hook Ediciones Musicales","type":"P"}],

etc etc. Also tried JSON.parse(JSON.stringify(data.body)) but I get the same output as with console.log.

How can I get the field's actual data, without losing its format?


Solution

  • Following @snak 's comment, I changed the console.log line to:

    console.log('Album information',util.inspect(data.body, false, null, true));

    and it seems to return everything fine, no [Object].

    (Make sure to include const util = require('util'); and install it with npm install util --save.)