我正在使用nodejs创建一个命令行应用程序。由于某种原因,每当我进入一首曲目(node liri.js spotify-this-song "enter track")时,我都会得到一个随机乐队的响应,该乐队名为“夜间仪式”,歌曲名为“未定义的东西”,专辑名为“大幻觉”。有没有人知道我哪里错了,或者我为什么会得到这些回复?
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);
};
});
} 发布于 2016-01-21 04:35:12
不要紧,我想通了。必须将查询更改为正确的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);和一个带有另一个params1的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;https://stackoverflow.com/questions/34909680
复制相似问题