简体   繁体   English

Node.js:无法调用未定义的方法版本(MySql)

[英]Node.js: Cannot call method release of undefined (MySql)

So great part of this is that this same code works on my development machine. 如此重要的部分是,相同的代码可在我的开发机器上使用。

I'm trying to deploy it to the production server, and i'm getting this error: Cannot call method release of undefined. 我正在尝试将其部署到生产服务器,并且出现此错误:无法调用未定义的方法版本。 This works on my box, so why is that not working in production is what confuses me. 这可以在我的盒子上工作,所以为什么不能在生产中工作使我感到困惑。 I downloaded the code with all the modules, then tried to do npm install, and still manually deploy express and mysql, but no luck. 我下载了所有模块的代码,然后尝试进行npm安装,仍然手动部署express和mysql,但是没有运气。

The infringing line is: connection.release(); 侵权行是:connection.release();。

If i rem that line, then it will just hang on anything trying to call connection. 如果我rem那条线,那么它将挂在试图呼叫连接的任何东西上。

The code: 编码:

process.env.TZ = 'UTC';
var express    = require("express");
var mysql      = require('mysql');

var pool      =    mysql.createPool({
    connectionLimit : 100, //important
    host     : 'localhost',
    user     : 'thedbuser',
    password : 'thedbpass',
    database : 'thedatabase',
    debug    :  false,
    timezone: 'utc'
});

var app = express();

function get_data(req,res) {

    pool.getConnection(function(err,connection){
        if (err) {
          connection.release();
          res.json({"code" : 100, "status" : "Error in connection database"});
          return;
        }   

        console.log('connected as id ' + connection.threadId);
        var id = Number(req.param("id"));

        connection.query("select * from thetable WHERE id = ?" , [id], function(err,rows){
            connection.release();
            if(!err) {
                res.json(rows);
            }           
        });

        connection.on('error', function(err) {      
              res.json({"code" : 100, "status" : "Error in connection database"});
              return;     
        });
  });
}

app.get("/id",function(req,res){

  get_data(req,res);

});

app.listen(3000);
pool.getConnection(function(err,connection){
    if (err) {
      connection.release();
      res.json({"code" : 100, "status" : "Error in connection database"});
      return;
    }   

So inside the if (err) block means there was an error. 因此,在if (err)块内部表示存在错误。 The node.js generic callback API semantic is when an err is passed to the callback function, the success/result variable is not guaranteed to also be provided. node.js通用回调API语义是在将err传递给回调函数时,不能保证也提供成功/结果变量。 In this case, since there was an error getting a connection, no connection object is passed to the callback - because the error prevented the connection from happening correctly. 在这种情况下,由于获取连接时出错,因此不会将任何连接对象传递给回调-因为该错误导致连接无法正确进行。 Thus you can probably safely just delete the connection.release() line but if you know there is a case where both err and connection will both be passed (I suspect there is no such case), you could guard your call to connection.release() with an if (connection) block or a logical operator like connection && connection.release() 因此,您可能可以放心地删除connection.release()行,但是如果您知道在某种情况下errconnection都将通过(我怀疑没有这种情况),则可以保护对connection.release()的调用connection.release()if (connection)块或逻辑运算符,例如connection && connection.release()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM