create pool for MySQL

hello I want to create a pool for MySQL but I got an error

Cannot read property 'createPool' of undefined notice,

I installed MySQL for my project

require("dotenv").config();

const { mysql } = require("mysql");

const isProduction = process.env.NODE_ENV === "production";

const connectionString = `mysql://${process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_DATABASE}`;

const pool = mysql.createPool({
  connectionString: isProduction ? 
  process.env.DATABASE_URL : connectionString,
  ssl: isProduction
});

module.exports = { pool };

Upvotes: 3

Views: 450

Answers (1)

jabaa
jabaa

Reputation: 6835

The npm package mysql doesn't export an object containing a property mysql. You either have to import the whole object

const mysql = require("mysql");

or only the function

const { createPool } = require("mysql");

and use it like

const pool = createPool({
  connectionString: isProduction ? 
  process.env.DATABASE_URL : connectionString,
  ssl: isProduction
});

Upvotes: 1

Related Questions