I have a strange problem in my nodejs website. I have a function to execute an SQL command. It works perfectly but after running server for a few days. It start to return an empty array!!! And when I restart the server it back to work again but still crashing after few days.
This is the error (the empty array error) :
TypeError: Cannot read property '0' of undefined
at Query._callback (/home/mikro/www/my_modules/database.js:47:27)
at Query.Sequence.end (/home/mikro/www/node_modules/mysql/lib/protocol/sequences/Sequence.js:86:24)
at /home/mikro/www/node_modules/mysql/lib/protocol/Protocol.js:226:14
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
and this is the function :
login: function (username, password, callback) {
db.query('SELECT id, password, expdate FROM users WHERE username=' +
mysql.escape(username) + ';', function (err, rows) {
var row = rows[0];
if (err) console.error(err)
if (!row)
callback(ERR_USER_NOT_FOUND, null);
else
bcrypt.compare(password, row.password, function (err, result) {
if (result){
var expdate = row.expdate;
var currDate = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
if (moment(currDate).isAfter(expdate) && row.id !== 1)
callback ('Please pay your monthly fee to be able to login', null);
else
callback(null, row)
}
else {
callback(ERR_WRONG_PASSWORD, null);
}
})
});
}
I have Ubuntu server and run pm2 with nginx proxy. my dependencies :
"dependencies": {
"bcrypt-nodejs": "latest",
"body-parser": "~1.16.0",
"connect-flash": "^0.1.1",
"cookie-parser": "~1.4.3",
"debug": "~2.6.0",
"ejs": "~2.5.5",
"express": "~4.14.1",
"express-session": "^1.15.1",
"moment": "^2.18.1",
"morgan": "~1.7.0",
"mysql": "latest",
"serve-favicon": "~2.3.2",
"socket.io": "latest",
"jwt-simple": "latest",
"jsonfile": "latest",
"express-mysql-session": "latest",
"express-rate-limit": "latest"
}
Update1
This is the connection code :
var mysql = require('mysql');
var db = mysql.createConnection(require('./config'));
db.connect();