Search code examples
javascriptmysqlknex.js

UnhandledPromiseRejectionWarning: KnexTimeoutError: Knex: Timeout acquiring a connection


I'm trying to connect the database to my Telegram bot and I want to use knex for it. I use MySQL. The database and the table are created. I also can see it on /phpMyAdmin page, so it works (I've used the same creds, that is used in my code). I've double-checked config data, it is right. Here's client creation code:

 this.knex = knex({
      client: "mysql",
      connection: {
        host: this.config.HOST,
        port: this.config.PORT,
        user: this.config.USERNAME,
        password: this.config.PASSWORD,
        database: this.config.DATABASE,
      },
//I've added next line, cuz I was thinking there is pool error, but it didn't work
          pool: { min: 0, max: 7 },
       });

I have it inside my class. Here's method, I'm trying to call (None of console.logs works):

  async getManagers() {
    const managers = await this.knex
      .select("*")
      .from("users")
      .where("isManager", 1)
      .then((data) => {
        console.log(data);
      });

    console.log(managers);
  }

I am absolutely sure that I have a table called user and the field isManager in it. Here's how I call it:

const dbDataService = new DBDataService(databaseConfiguration);
dbDataService.getManagers();

I keep getting this:

(node:31227) UnhandledPromiseRejectionWarning: KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
    at Client_MySQL.acquireConnection (/project/node_modules/knex/lib/client.js:348:26)
(node:31227) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 4)

If it helps, I'll add my dependency list:

  "dependencies": {
    "dotenv": "^11.0.0",
    "mysql": "^2.1.0",
    "knex": "^0.21.1",
    "log4js": "^6.3.0",
    "node-telegram-bot-api": "^0.56.0"
  }

I've tried to use different knex versions (1.0.1) and still get this error. I've already spent whole day to make this work, but still can't figure it out. Hope someone of you will help me. Thank you.


Solution

  • Actually, I messed up with PORT. I should be using 3306, but not 80.