Reputation: 2410
I'm trying to access each individual song in a playlist object returned by the spotify API. When I get the JSON object, it returns me a bunch of [Object] things in "items", which is where the tracks should be:
spotifyApi.getPlaylist(id, listID)
.then(function (data) {
// printing data from this line
console.log('The playlist contains these tracks', data.body);
}, function (err) {
console.log('Something went wrong!', err);
});
output:
Playlist name : thug lif3 Playlist ID: 41moBc7H5bWrR3iiY0Zu9Q
The playlist contains these tracks {
/*~~~ lots of skipped output ~~~*/
{ href: 'https://api.spotify.com/v1/users/...',
/*~~~ why [Object]? ~~~*/
items: [ [Object], [Object], [Object] ],
limit: 100
},
}
Are these [object] things a sign of a nullity, or is there some way I can parse them and get the data from them? The API explorer on Spotify's website shows the actual track objects after "Items", so I'm confused as to why my app request isn't doing the same.
Upvotes: 1
Views: 2846
Reputation: 81
This should do the trick:
var util = require('util');
...
spotifyApi.getPlaylist(id, listID)
.then(function (data) {
// printing data from this line
console.log('The playlist contains these tracks', util.inspect(data.body, { showHidden: true, depth: null });
}, function (err) {
console.log('Something went wrong!', err);
});
Upvotes: 1
Reputation: 203231
You can have console.log
output proper JSON:
console.log('The playlist contains these tracks %j', data.body);
(the %j
is documented here).
The regular output that console.log
generates for objects isn't proper JSON, and also limited to a certain object depth; after that, you'll get the [Object]
strings.
Upvotes: 2
Reputation: 6791
This is the rather normal behaviour of node. Either drill down programmatically or use util.inspect
link
Upvotes: 1
Reputation: 11171
That's just how deeply nested objects are printed to node's console. They're still available programmatically. In your case, use data.body.tracks.items
to access the objects.
If you want a pretty-printer, you can use bunyan, which'll make nested objects (and everything else) look really nice in the console.
Upvotes: 1