mysqlnode.jsmysql2

TypeError: pool.promise is not a function


I am using mysql2 library for connecting to database.

const pool = mysql.createPool({
  connectionLimit: process.env.DB_CONNECTION_LIMIT,
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_DATABASE,
  port: process.env.DB_PORT,
});

const promisePool = pool.promise();

on running above code i am getting TypeError: pool.promise is not a function, although this function is mentioned in docs: https://sidorares.github.io/node-mysql2/docs

also, i want to understand the need/definition of this function as i am still able to execute queries even if i remove this line. const promisePool = pool.promise();

    // working fine
    const [results, fields] = await pool.execute(query, [parameter1]);

Solution

  • You don't need to use the '.promise()' function. It is exposed only "to "upgrade" an existing non-promise connection to use promise." as per the documentation. MySQL2 already uses promises by default. Refer to the examples found here for the best way to get started with MySQL2 and Typescript.