【问题标题】:strange response from spotify api using node js使用节点 js 来自 spotify api 的奇怪响应
【发布时间】:2016-01-20 20:18:22
【问题描述】:

我正在使用 nodejs 创建一个命令行应用程序。出于某种原因,每当我输入一首曲目(node liri.js spotify-this-song "enter track")时,我都会收到一个名为 Nocturnal rites 的随机乐队的响应,名为 "something undefined" 的歌曲和名为 "grand vision" 的专辑。有谁知道我错在哪里,或者为什么我会收到这些回复?

  function spotifyIt(song) {
  spotify.search({ type: 'track', query: song }, function(err, data) {
    if ( err ) {
        console.log('Error occurred: ' + err);
        return;  //from spotify npm docs
    }
    else{
    var songInfo = data.tracks.items[0];
    var songResult = console.log(songInfo.artists[0].name)
                     console.log(songInfo.name)
                     console.log(songInfo.album.name)
                     console.log(songInfo.preview_url)
    console.log(songResult);
    };
  });
}  

【问题讨论】:

  • 你从哪里得到那个 spotify.search ?

标签: node.js spotify


【解决方案1】:

没关系,我想通了。必须将查询更改为正确的参数 []... 即它最终看起来像这样

function spotifyIt() {
  spotify.search({ type: 'track', query: params[1] }, function(err, data) {
    if ( err ) {
        console.log('Error occurred: ' + err);
        return;  //from spotify npm docs
    }
    else{
    var songInfo = data.tracks.items[0];
    var songResult = console.log(songInfo.artists[0].name)
                     console.log(songInfo.name)
                     console.log(songInfo.album.name)
                     console.log(songInfo.preview_url)
    console.log(songResult);
    };
  });
}  

我有一个全局变量 var params = process.argv.slice(2); 和一个带有另一个参数 [1] 的 switch 语句,因此它最终调用了第四个参数,即终端中歌曲标题的名称

switch(params[0]) {
  case "my-tweets":
    myTweets();
    break;
  case "spotify-this-song":
    if(params[1]){  //if a song is put named in 4th paramater go to function
    spotifyIt();
  } else {  //if blank call it blink 182's "whats my age again"
    spotifyIt("What\'s my age again");
  }
    break;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    相关资源
    最近更新 更多