调试静默JS功能失败
<小时/>我编写了一个 Node 连接函数,该函数连接成功,将数据插入数据库,但随后无法执行处理任何进一步错误的代码。
在这种情况下我应该如何调试?
<小时/>这是我的相关脚本:
var connection = createConnection();
connection.connect(function (err) {
if (err) return callback(new Error('Failed to connect'), null);
console.log('[Post]Connection with the officeball MySQL database opened...');
connection.query(
'INSERT INTO `officeball`.`sales_entries` SET category = ?, `group` = ?, date = ?, price = ?, customer = ?, seller = ?, commission = ?',
salesData),
function (err, rows, fields) {
if (err) console.log(err);
connection.destroy();
console.log('[Post]...Connection with the officeball MySQL database closed.');
}
});
这是我的控制台输出:
Application initializing...
Application successfully initialized!
Server running at http://127.0.0.1:8080/
User username1 is attempting to validate for a hit...
Connection with the Officeball MySQL database openned...
...Connection with the Officeball MySQL database closed.
User username1 validated and is ready!
[Post] Connection with the officeball MySQL database opened...
<小时/>
注意:您会注意到日志中存在两个连接,其中第一个连接成功打开和关闭。这就是验证。第二个连接未关闭,是由上述脚本启动的连接。
由于日志清楚地显示没有 fatal error ,因此您可以假设此处的所有内容都已正确定义。这是我到现在为止还没有遇到过的问题。通常,如果函数失败,程序就会结束并导致非常具体的错误。
最佳答案
正如 Aaron 所说,删除“salesData”后面的“)”并将其添加到“}”后面第 8 行。如果您打算将 RDB 与 Node 一起使用,请检查 https://github.com/luciotato/waitfor或https://github.com/luciotato/LiteScript
var connection = createConnection();
connection.connect(function (err) {
if (err) return callback(new Error('Failed to connect'), null);
console.log('[Post]Connection with the officeball MySQL database opened...');
connection.query(
'INSERT INTO `officeball`.`sales_entries` SET category = ?, `group` = ?, date = ?, price = ?, customer = ?, seller = ?, commission = ?',
salesData, function (err, rows, fields) {
if (err) console.log(err);
connection.destroy();
console.log('[Post]...Connection with the officeball MySQL database closed.');
});
});
另请阅读亚伦的第二条评论:
I strongly suggest installing and running jshint. For the snippet you posted here, it complains: "line 17, col 5, Expected an assignment or function call and instead saw an expression." which is the anonymous function that never gets passed anywhere. – Aaron Dufour
关于javascript - 语法完美的函数失败 'silently' - 我应该如何调试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22444848/