2016-01-20 31 views
0

我正在使用nodejs創建命令行應用程序。由於某種原因,每當我進入一首曲目(節點liri.js spotify-this-song「進入曲目」),我得到一個名爲「Nocturnal rites」的隨機樂隊的響應,名爲「something undefined」的歌曲和名爲「宏大幻覺」的專輯。有誰知道我錯在哪裏,或者我爲什麼會收到這些回覆?使用節點js的spotify API的奇怪響應

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

回答

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);和switch語句與另一個PARAMS [1],因此,它結束了調用第四個參數即歌曲標題在終端中的名稱

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;