我已经创建了一个 socket.io 聊天室,并且我正在使用一些数据库查询来插入和选择数据,但不幸的是我遇到了一些查询错误
我收到错误的文件的代码如下
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);
});
};
由于这段代码,我得到的错误是
请任何人都可以帮助我解决这个问题
非常感谢问候
原文由 Amina Rahman 发布,翻译遵循 CC BY-SA 4.0 许可协议
您的mysql连接似乎有错误。请先尝试正确连接
第 1 步:创建池(仅执行一次)
第 2 步:获取连接
检查此以获取更多详细信息 https://youtu.be/2CeAnrCsBQo
http://fearby.com/article/how-to-setup-pooled-mysql-connections-in-node-js-that-dont-disconnect/