我需要每隔 X 时间进行一次查询。我在运行节点版本0.8.12
和最新的节点mysql时运行以下代码
function testmysql()
{
mysqlCon.connect();
mysqlCon.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
mysqlCon.end();
}
var period = 2000;
var interval = setInterval(function(){
testmysql();
}, period);
我收到以下错误。
\nodetest\node_modules\mysql\lib\protocol\Protocol.js:95
.on('packet', function(packet) {
^
TypeError: Cannot call method 'on' of undefined
at Protocol._enqueue (\nodetest\node_modules\mysql\lib\protocol\Protocol.j
s:95:6)
at Protocol.handshake (\nodetest\node_modules\mysql\lib\protocol\Protocol.
js:37:41)
at Connection.connect (\nodetest\node_modules\mysql\lib\Connection.js:37:1
8)
at testmysql (\nodetest\index.js:1112:10)
at Timer.<anonymous> (\nodetest\index.js:1143:1)
at Timer.exports.setInterval.timer.ontimeout (timers.js:234:14)
虽然当 testmysql() 不在另一个函数中时,它运行正确。
我做错了什么?或者更确切地说我将如何执行这个循环查询?谢谢