Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 尝试在nodejs中实现连接池时连接未定义_Mysql_Node.js - Fatal编程技术网

Mysql 尝试在nodejs中实现连接池时连接未定义

Mysql 尝试在nodejs中实现连接池时连接未定义,mysql,node.js,Mysql,Node.js,我试图在node.js中实现连接池。在getConnection()方法中,如果我的池为空,我将创建一个具有给定连接数的池。 当控件指向var connection=pool[last]时,它会告诉我pool未定义。有人能帮帮我吗。谢谢 pool = null; last = 0; function connectionPool(connectionsNumber) { pool = []; for (var i=0; i < connectionsNumber; ++i) {

我试图在node.js中实现连接池。在
getConnection()
方法中,如果我的池为空,我将创建一个具有给定连接数的池。 当控件指向
var connection=pool[last]
时,它会告诉我
pool未定义。有人能帮帮我吗。谢谢

pool = null;
last = 0;
function connectionPool(connectionsNumber) {
  pool = [];
  for (var i=0; i < connectionsNumber; ++i) {
    pool.push(mysql.createConnection({
        host     : 'localhost',
        user     : 'root',
        password : 'Rileysteele1',
        dateStrings : true,
        database : 'twitter',
        port     : 3306
    }));
  }
}

function getConnection() {
  if (!pool) {
    initializeConnection(40);
  }
  var connection = pool[last];
  last++;
  if (last == pool.length)
    last = 0;
  return connection;
}

function initializeConnection() {
  pool = new connectionPool(2);
}

exports.initializeConnection = initializeConnection;
exports.getConnection = getConnection;
pool=null;
last=0;
函数connectionPool(ConnectionNumber){
池=[];
对于(变量i=0;i
我猜这是因为
connectionPool()
被视为构造函数(使用
new
),但它不是真正的构造函数,实际上是在内部设置
pool


删除
pool=new
部分,它应该可以工作。

我猜这是因为
connectionPool()
被视为一个构造函数(使用
new
),但它不是一个真正的构造函数,实际上是在内部设置
pool


删除
pool=new
部分,它应该可以工作。

节点mysql
支持。自己实现它似乎有些过分。

节点mysql
支持。自己实施似乎有些过分