我无法阅读属性'查询'未定义的

时间:2016-12-05 23:46:13

标签: node.js

我已经创建了一个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);
    });

};

由于此代码而导致的错误是

The image of the error i am getting

请有人帮我解决这个问题

非常感谢 此致

5 个答案:

答案 0 :(得分:4)

似乎你的mysql connection.try中存在错误,首先进行正确的连接

第1步:创建池(仅执行一次)

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
});

第2步:获取连接

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();
    }
});

查看此详细信息https://youtu.be/2CeAnrCsBQo

http://fearby.com/article/how-to-setup-pooled-mysql-connections-in-node-js-that-dont-disconnect/

答案 1 :(得分:1)

添加以下2个参数解决了我的问题

queueLimit
connectionLimit

exports.pool = mysqlCon.createPool({
    host: host,
    user: user,
    port: 3306,
    password: pass,
    database: db,
    queueLimit : 0, // unlimited queueing
    connectionLimit : 0 // unlimited connections 
});

注意:注意,这可能会破坏数据库。

答案 2 :(得分:0)

您需要处理错误,否则您可能会遇到类似的问题。特别是在这种情况下,您不会检查err回调中是否设置了pool.getConnection()。它很可能是设置的,也就是您没有有效connection对象的原因。

答案 3 :(得分:0)

mysql> ALTER USER'root'@'localhost'用mysql_native_password标识的'';

mysql>冲洗特权;

答案 4 :(得分:0)

用mysql2代替mysql解决了我的问题