gpt4 book ai didi

javascript - 为什么 [Object] 在 node.js 中打印到控制台而不是对象的内容?

转载 作者:行者123 更新时间:2023-11-30 08:37:59 25 4
gpt4 key购买 nike

我正在尝试访问 spotify API 返回的播放列表对象中的每一首歌曲。当我得到 JSON 对象时,它会在“项目”中返回一堆 [Object] 东西,这是轨道应该在的地方:

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);
});

输出:

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
},
}

这些 [object] 东西是无效的标志,还是有某种方法可以解析它们并从中获取数据? Spotify 网站上的 API 资源管理器在“项目”之后显示了实际的轨道对象,所以我很困惑为什么我的应用程序请求不这样做。

最佳答案

您可以让 console.log 输出正确的 JSON:

console.log('The playlist contains these tracks %j', data.body);

(%j 已记录 here)。

console.log 为对象生成的常规输出不是正确的 JSON,并且也仅限于特定的对象深度;之后,您将获得 [Object] 字符串。

关于javascript - 为什么 [Object] 在 node.js 中打印到控制台而不是对象的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29727233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com