i have make a socket.io chatroom and i am using some database queries to insert and select data but unfortunately i am getting some error of query
The code of the file on which i am getting error is as follow
var rooms = [];
module.exports.getUserFeeds = function (chatpage, socket, io, pool,async)
{
socket.on('senddata', function (data)
{
socket.user_id = data.user_id;
socket.room_id=data.room_id;
socket.room = 'room' + data.room_id;
rooms['room' + data.room_id] = 'room' + data.room_id;
socket.join('room' + data.room_id);
pool.getConnection(function (err, connection)
{
async.parallel([
function(callback)
{
connection.query('SELECT user_id,username FROM chat_users where user_id=' + data.user_id + '', function (error1, userdata)
{
if (error1) return callback(error1);
callback(null, userdata);
});
},
function (callback)
{
if(data.user_id)
connection.query('SELECT user_id,username FROM chat_users', function (error3, memdata)
{
if (error3) return callback(error3);
callback(null, memdata);
});
else
callback(null,null);
},
function(callback)
{
if(data.user_id)
connection.query('SELECT comment_id,comment,chat_users.user_id,username,comments.added FROM comments INNER JOIN chat_users ON comments.user_id=chat_users.user_id',function(error4,converdata){
if (error4) return callback(error4);
callback(null, converdata);
});
else
callback(null,null);
}
], function (err, results)
{
if(err) throw err;
socket.emit('chatdata',
{
memdata:results[1],
converdata:results[2],
});
socket.broadcast.to('room'+ data.room_id +'').emit('newuser', {userdata:results[0]});
connection.release();
});
});
});
socket.on('sendcomment', function (data)
{
pool.getConnection(function (err, connection)
{
connection.query('INSERT INTO comments (user_id,comment,added) VALUES (' + data.user_id + ',"' + data.msg + '","' + data.datetime + '")', function (err, result)
{
if (err) throw err;
async.parallel([
function (callback)
{
connection.query('SELECT comments.*,username,comments.added from comments JOIN chat_users ON comments.user_id=chat_users.user_id WHERE comments.comment_id=' + result.insertId + '', function (err2, comments)
{
if (err2) return callback(err2);
callback(null, comments);
});
},
function (callback)
{
connection.query('SELECT count(comment_id) as tot_comment from comments', function (err3, comment_count)
{
if (err3) return callback(err3);
callback(null, comment_count);
});
},
], function (err, results)
{
if (err) throw err;
if (results[0])
{
chatpage. in('room'+ data.room_id +'').emit('showcomment',
{
room_comment: results[0],
comment_count: results[1]
});
}
connection.release();
});
});
});
});
socket.on('disconnect', function ()
{
console.log("user disconnected");
pool.getConnection(function (err, connection)
{
connection.query('DELETE from chat_users where user_id='+socket.user_id+'', function (err, removeuser)
{
if (err) throw err;
});
connection.query('DELETE from comments where user_id='+socket.user_id+'', function (err, removecomments)
{
if (err) throw err;
});
connection.release();
});
socket.broadcast.to('room'+ socket.room_id +'').emit('removeuser', {user_id:socket.user_id});
socket.leave(socket.room);
});
};
and the error i am getting because of this code is
The image of the error i am getting
please can anyone help me regarding this issue
Thanks a lot Regards
It seems there is error in your mysql connection.try make proper connection first
Step 1: Create the pool (only do this once)
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'hostname',
user : 'username',
password : 'password',
database : 'db_name',
connectionLimit : 10, // this is the max number of connections before your pool starts waiting for a release
multipleStatements : true // I like this because it helps prevent nested sql statements, it can be buggy though, so be careful
});
Step 2: Get a connection
pool.getConnection(function (err, conn) {
if (err)
return res.send(400);
// if you got a connection...
conn.query('SELECT * FROM table WHERE id=? AND name=?', [id, name], function(err, rows) {
if(err) {
conn.release();
return res.send(400, 'Couldnt get a connection');
}
// for simplicity, just send the rows
res.send(rows);
// CLOSE THE CONNECTION
conn.release();
}
});
check this for more detail https://youtu.be/2CeAnrCsBQo
http://fearby.com/article/how-to-setup-pooled-mysql-connections-in-node-js-that-dont-disconnect/
queueLimit
connectionLimit
exports.pool = mysqlCon.createPool({
host: host,
user: user,
port: 3306,
password: pass,
database: db,
queueLimit : 0, // unlimited queueing
connectionLimit : 0 // unlimited connections
});
Note : Be careful, this might break your database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With