我对节点还不熟悉,我正在通过node.js处理一个API调用,我有点搞不懂为什么会这样。我已经通过节点轻松地完成了其他API调用,因为很容易找到如何针对各个字段等。但是我从来没有得到过与spotify API的链接,我也不明白data.tracks.items.artists.name是如何给我命名的?
我知道这是一个无知的问题,但我真的很想了解它是如何工作的,而不仅仅是让它发挥作用。
function song() {
var nodeArgs = process.argv;
var SongName = "";
for (var i = 3; i < nodeArgs.length; i++) {
if (i > 3 && i < nodeArgs.length) {
SongName = SongName + "+" + nodeArgs[i];
}
else {
SongName += nodeArgs[i];
}
}
var Spotify = require('node-spotify-api');
var spotify = new Spotify({
id: "id",
secret: "secret"
});
spotify.search({ type: 'track', query: SongName, limit: 1 }, function (err, data) {
if (err) {
SongName = "";
console.log("Artist: " + songData.artists[0].name);
console.log("Song Title: " + songData.name);
console.log("Preview Track: " + songData.preview_url);
console.log("Album: " + songData.album.name);
song();
}
for (var i = 0; i < data.tracks.items.length; i++) {
var songData = data.tracks.items[i];
console.log("Artist: " + songData.artists[0].name);
console.log("Song Title: " + songData.name);
console.log("Preview Track: " + songData.preview_url);
console.log("Album: " + songData.album.name);
}
});
}
发布于 2019-01-22 20:15:21
简而言之-- track端点使用同样包含艺术家对象的Object Model
进行响应--这是一个艺术家对象数组,其中艺术家对象包含键name
。
参考文献:https://developer.spotify.com/documentation/web-api/reference/tracks/get-track/
从他们的API文档
响应对象包含
KEY VALUE | TYPE | VALUE DESCRIPTION
---
artists | an array of simplified | The artists who performed the track.
| artist objects | information about the artist.
艺术家对象
artist object (simplified)
KEY VALUE | TYPE | VALUE DESCRIPTION
---
external_urls | an external URL object | Known external URLs for this artist.
href | string | A link to the Web API endpoint providing full details of the artist.
id | string | The Spotify ID for the artist.
name | string | The name of the artist
type | string | The object type: "artist"
uri | string | The Spotify URI for the artist.
https://stackoverflow.com/questions/54319848
复制