来自使用节点js的spotify api的奇怪响应

时间:2016-01-20 20:18:22

标签: node.js spotify

我正在使用nodejs创建一个命令行应用程序。出于某种原因,每当我进入一个轨道(节点liri.js spotify-这首歌“进入轨道”)时,我得到一个名为Nocturnal rites的随机乐队的响应,名为“something undefined”的歌曲和名为“grand illusion”的专辑。有谁知道我错在哪里,或者为什么我得到这些回复?

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

1 个答案:

答案 0 :(得分:0)

没关系,我明白了。不得不将查询更改为正确的params [] ...即最终看起来像这样

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;