在MySQL(带池连接)和Node.js中执行查询参数

时间:2017-09-29 15:13:29

标签: javascript jquery mysql node.js callback

我正在写作,因为我在使用Node.js和池连接执行MySQL查询时遇到问题。 我想有并发查询,所以我使用池连接,但我没有显示错误就无法执行我的查询。 此外,在显示错误后,我不允许运行任何其他查询,因此它导致了很多问题。 这是 base.js 文件,其中包含我从另一个问题中获取的一些代码。

var mysql   = require("mysql");

var pool = mysql.createPool({
    connectionLimit : 10, 
    host     : 'localhost',
    user     : 'myuser',
    password : 'mypassword',
    database : 'mydatabase',
    debug    :  false
});

pool.on('acquire',function(connection) {
    console.log("Connection "+connection.threadId+" acquired");
})

pool.on('enqueue',function() {
    console.log("..Waiting for available connection slot");
})

pool.on('release',function(connection) {
    console.log("Connection "+connection.threadId+" released");
})


var DB = (function () {
    function _query(query,params,callback) {
        pool.getConnection(function (err, connection) {
            if (err) {
                //connection.release(); 
                callback(null, err);
                throw err;
            }

            connection.query(query,params,function(err,rows) {
                connection.release();
                if (!err) callback(rows);
                else callback(null,err);
            });

            connection.on('error', function (err) {
                connection.release();
                callback(null, err);
                throw err;
            });
        });
    };
    return { query: _query };
})(); 

module.exports = DB;

这是另一个文件 test.js ,我想在其中运行我想要的所有查询(在这种情况下我只放一个)。

var DB = require('./base.js');

DB.query("SELECT table_name from information_schema.tables",null,function(data, error) {
    console.log(data)
    callback(data, error);
});

最后,这是我获得的输出。

root@backbox-VM:/home/backbox/Desktop/mysqltest# node test.js 
Connection 38 acquired
Connection 38 released
[ RowDataPacket { table_name: 'CHARACTER_SETS' },
  RowDataPacket { table_name: 'COLLATIONS' },
  RowDataPacket { table_name: 'COLLATION_CHARACTER_SET_APPLICABILITY' },
  RowDataPacket { table_name: 'COLUMNS' },
  RowDataPacket { table_name: 'COLUMN_PRIVILEGES' },
  RowDataPacket { table_name: 'ENGINES' },
  RowDataPacket { table_name: 'EVENTS' },
  RowDataPacket { table_name: 'FILES' },
  RowDataPacket { table_name: 'GLOBAL_STATUS' },
  RowDataPacket { table_name: 'GLOBAL_VARIABLES' },
  RowDataPacket { table_name: 'KEY_COLUMN_USAGE' },
  RowDataPacket { table_name: 'OPTIMIZER_TRACE' },
  RowDataPacket { table_name: 'PARAMETERS' },
  RowDataPacket { table_name: 'PARTITIONS' },
  RowDataPacket { table_name: 'PLUGINS' },
  RowDataPacket { table_name: 'PROCESSLIST' },
  RowDataPacket { table_name: 'PROFILING' },
  RowDataPacket { table_name: 'REFERENTIAL_CONSTRAINTS' },
  RowDataPacket { table_name: 'ROUTINES' },
  RowDataPacket { table_name: 'SCHEMATA' },
  RowDataPacket { table_name: 'SCHEMA_PRIVILEGES' },
  RowDataPacket { table_name: 'SESSION_STATUS' },
  RowDataPacket { table_name: 'SESSION_VARIABLES' },
  RowDataPacket { table_name: 'STATISTICS' },
  RowDataPacket { table_name: 'TABLES' },
  RowDataPacket { table_name: 'TABLESPACES' },
  RowDataPacket { table_name: 'TABLE_CONSTRAINTS' },
  RowDataPacket { table_name: 'TABLE_PRIVILEGES' },
  RowDataPacket { table_name: 'TRIGGERS' },
  RowDataPacket { table_name: 'USER_PRIVILEGES' },
  RowDataPacket { table_name: 'VIEWS' },
  RowDataPacket { table_name: 'INNODB_LOCKS' },
  RowDataPacket { table_name: 'INNODB_TRX' },
  RowDataPacket { table_name: 'INNODB_SYS_DATAFILES' },
  RowDataPacket { table_name: 'INNODB_FT_CONFIG' },
  RowDataPacket { table_name: 'INNODB_SYS_VIRTUAL' },
  RowDataPacket { table_name: 'INNODB_CMP' },
  RowDataPacket { table_name: 'INNODB_FT_BEING_DELETED' },
  RowDataPacket { table_name: 'INNODB_CMP_RESET' },
  RowDataPacket { table_name: 'INNODB_CMP_PER_INDEX' },
  RowDataPacket { table_name: 'INNODB_CMPMEM_RESET' },
  RowDataPacket { table_name: 'INNODB_FT_DELETED' },
  RowDataPacket { table_name: 'INNODB_BUFFER_PAGE_LRU' },
  RowDataPacket { table_name: 'INNODB_LOCK_WAITS' },
  RowDataPacket { table_name: 'INNODB_TEMP_TABLE_INFO' },
  RowDataPacket { table_name: 'INNODB_SYS_INDEXES' },
  RowDataPacket { table_name: 'INNODB_SYS_TABLES' },
  RowDataPacket { table_name: 'INNODB_SYS_FIELDS' },
  RowDataPacket { table_name: 'INNODB_CMP_PER_INDEX_RESET' },
  RowDataPacket { table_name: 'INNODB_BUFFER_PAGE' },
  RowDataPacket { table_name: 'INNODB_FT_DEFAULT_STOPWORD' },
  RowDataPacket { table_name: 'INNODB_FT_INDEX_TABLE' },
  RowDataPacket { table_name: 'INNODB_FT_INDEX_CACHE' },
  RowDataPacket { table_name: 'INNODB_SYS_TABLESPACES' },
  RowDataPacket { table_name: 'INNODB_METRICS' },
  RowDataPacket { table_name: 'INNODB_SYS_FOREIGN_COLS' },
  RowDataPacket { table_name: 'INNODB_CMPMEM' },
  RowDataPacket { table_name: 'INNODB_BUFFER_POOL_STATS' },
  RowDataPacket { table_name: 'INNODB_SYS_COLUMNS' },
  RowDataPacket { table_name: 'INNODB_SYS_FOREIGN' },
  RowDataPacket { table_name: 'INNODB_SYS_TABLESTATS' } ]
/home/backbox/Desktop/mysqltest/node_modules/mysql/lib/protocol/Parser.js:80
        throw err; // Rethrow non-MySQL errors
        ^

ReferenceError: callback is not defined
    at /home/backbox/Desktop/mysqltest/test.js:5:5
    at Query._callback (/home/backbox/Desktop/mysqltest/base.js:156:27)
    at Query.Sequence.end (/home/backbox/Desktop/mysqltest/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)
    at Query._handleFinalResultPacket (/home/backbox/Desktop/mysqltest/node_modules/mysql/lib/protocol/sequences/Query.js:139:8)
    at Query.EofPacket (/home/backbox/Desktop/mysqltest/node_modules/mysql/lib/protocol/sequences/Query.js:123:8)
    at Protocol._parsePacket (/home/backbox/Desktop/mysqltest/node_modules/mysql/lib/protocol/Protocol.js:279:23)
    at Parser.write (/home/backbox/Desktop/mysqltest/node_modules/mysql/lib/protocol/Parser.js:76:12)
at Protocol.write (/home/backbox/Desktop/mysqltest/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/home/backbox/Desktop/mysqltest/node_modules/mysql/lib/Connection.js:103:28)
at emitOne (events.js:115:13)

提前感谢您的帮助。

0 个答案:

没有答案