I need to make a query every X amount of time. I am running node version 0.8.12
and the latest node mysql When running the following code
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);
I get the following error.
\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)
Though when testmysql() is not in another function it runs correctly.
What am I doing incorrectly? or rather how would I perform this loop query? Thanks
Try this instead - it's better design and it should hopefully resolve your error:
function testmysql(mysqlConnection)
{
mysqlConnection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
}
var period = 2000;
var interval = setInterval(function(){
var mysqlCon = mysql.createConnection({host:'localhost',user:'testadmin',password:'pass',database:'mydb'});
mysqlCon.connect();
testmysql(mysqlCon);
mysqlCon.end();
}, period);
ok... I deconstructed the project line by line... and the code that was causing it was this -
var EventEmitter = require('events').EventEmitter
, on = EventEmitter.prototype.on;
EventEmitter.prototype.on = function () {
this._maxListeners = Infinity;
on.apply(this, arguments);
};
I was using this snippet because node_redis was throwing an error or warning of a memory leak due to the max listeners limit every time someone would join as I am using redisstore with socket.io. I Removed the code and I am no longer getting the memory leak errors perhaps because I took the latest node_redis version.
So sorry for the trouble and thanks for the help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.