2016-02-18 33 views
0

我的代碼:Node.js的控制檯調試消息,是怪異

var prompt = require('sync-prompt').prompt; 
var input = prompt('What is it you are after? '); 
var mkdirp = require('mkdirp'); 
var fs = require('fs'); 
var url = require('url'); 
var http = require('http'); 
var exec = require('child_process').exec; 
var spawn = require('child_process').spawn; 
var unzip = require('unzip'); 
console.log('Okay, we will do our best to find ' + input + ', ' + ' ' + 'hold on.'); 
var mysql = require('mysql'); 
var connection = mysql.createConnection({ 
    host: 'localhost', 
    user: 'root', //just using root for my personal testing. 
    password: 'root', 
    database: 'Sonic' 
}); 
connection.query('SELECT * FROM Sonic_URL WHERE Name="' + input + '"', function(err, rows, fields) { 
    if (err) throw err; 
    var Name = (fields[0].Uname); 
    if (Name === input) { 
     console.log("Found it! Just getting the download!"); 
    } 
    } 
    //download progress bar 
    var ProgressBar = require('progress'); 
    var bar = new ProgressBar(':bar', { 
     total: 10 
    }); 
    var timer = setInterval(function() { 
     bar.tick(); 
     if (bar.complete) { 
      console.log('\ncomplete\n'); 
      clearInterval(timer); 
     } 
    }, 100); 
    //Building the download// 
    var spawn = require('child_process').spawn; 
    //Location of download. 
    var DOWNLOAD_DIR = './Sonic-Downloads'; 
    //Making sure the Dir exists 
    var mkdir = 'mkdir ' + DOWNLOAD_DIR; 
    var child = exec(mkdir, function(err, stdout, stderr) { 
     // if (err) console.log('Awww snap! d') Ingnore this 
     download_file_httpget((rows[0].Address)); 
    }); 
} 
// Function to download file using HTTP.get 
var download_file_httpget = function(sqlres) { 
    var options = { 
     host: url.parse(sqlres).host, 
     //port: , 
     path: url.parse(sqlres).pathname 
    }; 
    var sqlres = url.parse(sqlres).pathname; 
    var file = fs.createWriteStream(DOWNLOAD_DIR + sqlres); 
    http.get(options, function(res) { 
     res.on('data', function(data) { 
      file.write(data); 
     }).on('end', function() { 
      file.end(); 
      console.log('Succsesfully downloaded ' + 
       namematch); 
      // Other things that need to be closed go here 
      process.exit(); 
     }); 
    }); 
}; 
}); 

//shut down code 
process.on('SIGINT', function() { 
    console.log(
     "\nGoodbye!a!" 
    ); 
    // some other closing procedures go here 
    process.exit(); 
}) 

當我運行這段代碼與節點命令我得到這個。

SyntaxError: missing) after argument list 
at exports.runInThisContext (vm.js:53:16) 
at Module._compile (module.js:414:25) 
at Object.Module._extensions..js (module.js:442:10) 
at Module.load (module.js:356:32) 
at Function.Module._load (module.js:313:12) 
at Function.Module.runMain (module.js:467:10) 
at startup (node.js:136:18) 
at node.js:963:3 

做完它所說的,然後我得到這個。

SyntaxError: Unexpected token var 
at exports.runInThisContext (vm.js:53:16) 
at Module._compile (module.js:414:25) 
at Object.Module._extensions..js (module.js:442:10) 
at Module.load (module.js:356:32) 
at Function.Module._load (module.js:313:12) 
at Function.Module.runMain (module.js:467:10) 
at startup (node.js:136:18) 
at node.js:963:3 

不知道爲什麼它這樣做,我已經檢查,以確保所有,{},[],()和等馬相匹配,關閉,等等,任何幫助將不勝感激。

+2

如果縮進的代碼正確,錯誤是相當明顯 – adeneo

+0

一切都很好地縮進,直到我來把它放在堆棧溢出,當壓痕抹殺。 –

+0

如下所述,似乎有幾個額外的curlybraces等,我會縮進爲你。 – adeneo

回答

0
if (Name === input) { 
    console.log("Found it! Just getting the download!"); 
} 
} 

取出一個支架

connection.query('SELECT * FROM Sonic_URL WHERE Name="' + input + '"',  function(err, rows, fields) { 
if (err) throw err; 
var Name = (fields[0].Uname); 
if (Name === input) { 
    console.log("Found it! Just getting the download!"); 
} 

//download progress bar 
var ProgressBar = require('progress'); 
var bar = new ProgressBar(':bar', { 
total: 10 
}); 
var timer = setInterval(function() { 
bar.tick(); 
if (bar.complete) { 
    console.log('\ncomplete\n'); 
    clearInterval(timer); 
} 
}, 100); 
//Building the download// 
var spawn = require('child_process').spawn; 
//Location of download. 
var DOWNLOAD_DIR = './Sonic-Downloads'; 
//Making sure the Dir exists 
var mkdir = 'mkdir ' + DOWNLOAD_DIR; 
var child = exec(mkdir, function(err, stdout, stderr) { 
// if (err) console.log('Awww snap! d') Ingnore this 
download_file_httpget((rows[0].Address)); 
}); 
}) 
+0

謝謝!修復它。 –