我试图在全球范围内将Node项目安装到我的机器上。它使用sqlite3来查询我的itunes.db文件。包结构如下所示:
/node_modules
db.js
index.js
itunes.js
package.json
从本地目录运行时,我可以正常运行该应用程序。我使用别名" itunes"运行应用程序。
C:\Users\Khalil\Documents\itunes-cmd\npm install -g
C:\Users\Khalil\Documents\itunes-cmd\itunes play shut me down
--- Multiple Songs Found -----
[0] - Shut Me Down - Rowland S. Howard - Teenage Snuff Film
[1] - Shut Me Down - Rowland S. Howard - Pop Crimes
Which song # you want to play [Enter #]: 1
Playing 'Shut Me Down' by Rowland S. Howard
但是当我离开项目目录并尝试运行应用程序时,我收到错误。
cd ..
cd ..
itunes play shut me down
C:\Users\Khalil\AppData\Roaming\npm\node_modules\itunes-cmd\node_modules\sqlite3\lib\trace
.js:28
throw err;
^
TypeError: Cannot read property 'length' of undefined
at C:\Users\Khalil\AppData\Roaming\npm\node_modules\itunes-cmd\index.js:120:22
at C:\Users\Khalil\AppData\Roaming\npm\node_modules\itunes-cmd\db.js:132:11
at Statement.errBack (C:\Users\Khalil\AppData\Roaming\npm\node_modules\itunes-cmd\node
_modules\sqlite3\lib\sqlite3.js:16:21)
--> in Database#all('SELECT * FROM ITUNES_LIBRARY WHERE TITLE LIKE ? ', '%shut me down%',
[Function])
at DatabaseController.findSongByTitle (C:\Users\Khalil\AppData\Roaming\npm\node_module
s\itunes-cmd\db.js:131:8)
at searchSong (C:\Users\Khalil\AppData\Roaming\npm\node_modules\itunes-cmd\index.js:11
6:12)
at Object.<anonymous> (C:\Users\Khalil\AppData\Roaming\npm\node_modules\itunes-cmd\ind
ex.js:158:21)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:118:18)
我发现当我运行应用程序时,findSongByTitle返回的结果通常会返回一个歌曲对象数组,但是当我收到错误时会打印出未定义的内容。
// Search database for song title
db.findSongByTitle(searchQuery, function(result){
console.log(result); // prints undefined
// If only one result, play that song
if(result.length == 1){
playTrackFromLocation(result[0].LOCATION, result[0].TITLE, result[0].ARTIST);
// Otherwise, ask the user which song they want to play (by number)
} else {
console.log("--- Multiple Songs Found -----");
for(var i = 0; i < result.length; i++){
console.log(chalk.yellow("[" + i + "] - " + result[i].TITLE + " - " + result[i].ARTIST + " - " + result[i].ALBUM));
}
// Wait for user's response.
console.log("");
var songSelection = readlineSync.question('Which song # you want to play [Enter #]: ');
if(!isNaN(songSelection)){
// Play selected track
console.log("");
playTrackFromLocation(result[songSelection].LOCATION, result[songSelection].TITLE, result[songSelection].ARTIST);
} else {
console.log(chalk.red("No valid song # selected"));
}
}
});
findSongByTitle函数如下所示:
DatabaseController.prototype.findSongByTitle = function(search, callback){
db.all("SELECT * FROM ITUNES_LIBRARY WHERE TITLE LIKE ? ", '%' + search + '%', function(err, queryResults) {
callback(queryResults);
});
}
完全不确定我是否遗漏了这里显而易见的东西,我只是不明白为什么它在我的全球安装路径上无法正常工作:
C:\Users\Khalil\AppData\Roaming\npm\node_modules\itunes-cmd
具有所有本地安装文件的精确副本。 我的其余代码位于GitHub。