获取connection.release不是节点中的函数错误

时间:2017-12-27 10:56:53

标签: javascript mysql node.js

我正在尝试设置连接池,并希望在完成查询后释放连接。

我如何实施:

dbConnection.getConnection( function(err, connection){

                dbConnection.release();

    if (err) {
      console.log("db error ", err);
      return callback(err);
      //connection.release();
    }else {

      var memberId= member.member_id;

      return dbConnection.query("Select * from tableName, function(err, result,fields){



        if (err) {
          return callback(err);
        }else {


          return callback(null, result);
        }

      });


    }

    dbConnection.on('error', function(err) {
           console.log("db on error ");
           callback(err);
           return;
    });

我收到错误:

TypeError: dbConnection.release is not a function
    at Query._callback (/home/itstym/node_js/perb/models/member.js:26:22)
    at Query.Sequence.end (/home/itstym/node_js/perb/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)
    at Query._handleFinalResultPacket (/home/itstym/node_js/perb/node_modules/mysql/lib/protocol/sequences/Query.js:139:8)
    at Query.EofPacket (/home/itstym/node_js/perb/node_modules/mysql/lib/protocol/sequences/Query.js:123:8)
    at Protocol._parsePacket (/home/itstym/node_js/perb/node_modules/mysql/lib/protocol/Protocol.js:279:23)
    at Parser.write (/home/itstym/node_js/perb/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (/home/itstym/node_js/perb/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/home/itstym/node_js/perb/node_modules/mysql/lib/Connection.js:103:28)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)

但我已经通过互联网上的大量例子,所有人都在做同样的事情。我错过了什么吗?

dbconnection.js

var dbconnection = mysql.createPool({

  connectionLimit : 10, //important
  host:'localhost',
  user:'root',
  database:'perb',
  password: 'dbandc',
  debug    :  false
});


module.exports=dbconnection;

2 个答案:

答案 0 :(得分:1)

您希望对从回调传递的连接变量调用release(),而不是在池本身上调用dbConnection.getConnection( function(err, connection){ connection.release(); 。前两行应该是:

dbConnection

我基本上只是在第3行将connection更改为File dir = new File(AppDelegate.sharedDelegate().getExternalCacheDir(), "documentfolder"); dir.mkdirs();

答案 1 :(得分:0)

请使用connection.release()代替dbConnection.release();

请参考  https://www.npmjs.com/package/mysql#pooling-connections