为什么这行得通? Spotify API 通话 node.js
Why does this work? Spotify API call node.js
我是 node 的新手,正在通过 node.js 进行 API 调用,我有点困惑为什么会这样。我通过节点轻松完成了其他 API 调用,因为很容易弄清楚如何定位各个领域等。但是我从来没有用 spotify API 得到 link 并且我困惑 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);
}
});
}
简答 -
track api 端点以 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.
我是 node 的新手,正在通过 node.js 进行 API 调用,我有点困惑为什么会这样。我通过节点轻松完成了其他 API 调用,因为很容易弄清楚如何定位各个领域等。但是我从来没有用 spotify API 得到 link 并且我困惑 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);
}
});
}
简答 -
track api 端点以 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.