I am trying to work on my database but after establishing the connection between the server, I am unable to execute any query using the execute or the query function. The error says that it is not a function however when I check the GitHub repo, the commands are present and also in the readme the functions are shown as demonstration. Can anyone please help me resolve this issue? Below is the code along with the error. I will also share the link from which I have got this module information.
I have not provided the user and password for obvious reasons. Below is the Code:
import express from "express";
import mysql from "mysql2/promise";
try {
const server_config = {
host: 'localhost',
database: "sample"
};
const connection = mysql.createConnection(server_config);
const [results, fields] = await connection.execute("CREATE TABLE sample (name VARCHAR(25), id INT PRIMARY KEY)");
console.log(results);
console.log(fields);
} catch(error){
console.log(error);
}
Error:
TypeError: connection.execute is not a function
at file:///Users/klsharma22/Desktop/API/API.js:12:48
at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:337:24)
at async loadESM (node:internal/process/esm_loader:34:7)
at async handleMainPromise (node:internal/modules/run_main:106:12)
Supported links:
GitHub Repo for Mysql2
mysql2 Documentation
I just want to work with the database after connecting to the server, however, when I am working according to the documentation I am not able to execute the functions as mentioned in the documentation and the GitHub repo.
Krish Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.