Mysql getConnection()不返回Node.js中的回调函数

时间:2019-04-17 22:55:46

标签: mysql node.js database

我正在尝试从nodejs中的mysql连接池获取连接。但是该池的getConnection方法不会返回回调函数。 (因此它不会显示任何错误或返回连接)。但是我的mysql工作台显示已建立连接。仍然在使用连接时,它在回调中不返回任何内容。

   var pool = mysql.createPool({
      connectionLimit: 10,
      host: "localhost",
      database: "database",
      user: "root",
      password: "password"
   });

   exports.query = function (query, params, callback) {
      pool.getConnection(function (err, connection) {
        if (err) {

            console.log(err);
            connection.release();
            callback(err.code, null);
        }
        console.log("connected");
        connection.query(query, params, function (err, results) {
            connection.release();
            if (!err) {
                callback(null, results);

            }
            else {

                callback(err.code, null);

            }

        });

        connection.on('error', function (err) {
            connection.release();
            callback(err.code, null);
        });

    });           

}

这是我使用数据库进行查询的地方。

 exports.getLevels= function(callback){
    var query = 'SELECT * FROM levels';
    db.query(query,null, function (err, results) {
       if (err) {
          console.log(err);
       }
       console.log('reached');
       callback(err, results);
    });
 }

1 个答案:

答案 0 :(得分:1)

尝试此操作可能会有所帮助

我正在使用此npm软件包 mysql使用此命令

npm i --save mysql

我认为您没有本地用户和远程用户,或者您的mysql实例未运行。确保已创建一个本地用户,您可以使用nodejs脚本访问该本地用户,可以从here获取帮助,并可以使用this检查连接。

const MySQL_DB = require( "mysql" );
let DB_Pool = MySQL_DB.createPool( {
    connectionLimit: 15,
    multipleStatements: true,
    host: "localhost",
    user: "root",
    password: "abcdefgh",
    database: "working_db",
    port: "3306"
} );

function GetDatabaseResponse( query ) {

    return new Promise( ( resolve, reject ) => {
        DB_Pool.getConnection( ( ConnectionErrorMessage, DB_LocalConnection ) => {
            if ( ConnectionErrorMessage ) {
                console.log( "DataBase Connection Failed" );
                console.log( ConnectionErrorMessage );
                resolve( ConnectionErrorMessage );
                return;

            } else {

                DB_LocalConnection.query( query, ( ProcedureErrorMessage, ProcedureCallResult ) => {
                    DB_LocalConnection.release();
                    if ( ProcedureErrorMessage ) {
                        console.log( "DataBase Query Execution Failed" );
                        console.log( DB_ProcedureCall );
                        console.log( ProcedureErrorMessage );
                        resolve( ProcedureErrorMessage );
                        return;

                    } else {
                        resolve( ProcedureCallResult );
                        return;
                    }
                } );
            }
        } );
    } );
}

( async () => {
    let query = 'SELECT * FROM levels';
    let Result = await GetDatabaseResponse( query );
    console.log( Result );
} )();

这是您的代码实现,对我而言很好。

databaseFile.js

const mysql = require( "mysql" );
var pool = mysql.createPool({
    connectionLimit: 15,
    multipleStatements: true,
    host: "localhost",
    user: "root",
    password: "abcdefgh",
    database: "working_db",
    port: "3306"
 });

module.exports =( query, callback) => { 

    pool.getConnection((err, connection) => {
      if (err) {
          console.log(err);
          connection.release();
          callback(err.code, null);
      }
      console.log("connected");
      connection.query(query,  (err, results) => {
          connection.release();
          if (!err) {
              callback(null, results);
          }
          else {
              callback(err.code, null);
          }
      });

      connection.on('error',(err) => {
          connection.release();
          callback(err.code, null);

      });

  });
}

index.js

let GetBataBaseResponse = require("./databaseFile");

GetBataBaseResponse("Select * from tblagents",(error, result) => {
    console.log("error",error);
    console.log("result",result);
});

如果仍然有问题要给我发消息。