如何从spotify npm包中提取数据?我一直不定

时间:2017-12-05 15:24:56

标签: node.js npm


===================This is my code below=========================

function spotifyThisSong() {
        var spotify = new Spotify({
            id: 'myid',
            secret: 'mysecret'
        });
        var songName = process.argv[3];
        if(!songName){
                    songName = "What's my age again";
                }
        var params = songName;
          spotify.search({ type: 'track', query: params }, function(err, data) {
            if ( err ) {
                console.log('Error occurred: ' + err);
                return;  //from spotify npm docs
            }
            else{       
            console.log(data);
            };
          }); 
    }
===================END OF CODE=========================


keeps giving me undefine.  I need to extract the song name, year, album and its url.  Thanks in advance.

1 个答案:

答案 0 :(得分:0)

  //So this is what I found.  This actually involves a few things.
  //FIRST, capture user-input,
  //SECOND, npm install spotify, and use the template as suggested in the DOCS. 
  //THIRD, parse through the JSON correctly as done below.

  //HOPE this helps someone.  (dont forget your keys from spotify)


  var songName = process.argv[3]; //capture userInput and query it below 

 params = songName;
    spotify.search({ type: 'track', query: params }, function(err, data) {
        if ( err ) {
            console.log('Error occurred: ' + err);
            return;  
        }
        else{
            output = space + "================= DATA HERE ==================" + 
            space + "Song Name: " + "'" +songName.toUpperCase()+ "'" +
            space + "Album Name: " + data.tracks.items[0].album.name +
            space + "Artist Name: " + data.tracks.items[0].album.artists[0].name +  
            space + "URL: " + data.tracks.items[0].album.external_urls.spotify + "\n\n\n";
            console.log(output);    
            };
    });