var mysql = require("mysql");
import React from "react";
export async function executeMySqlQuery(myQuery) {
try {
console.log("Running query...");
var connection = mysql.createConnection({
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USERNAME,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
});
connection.connect();
connection.query(myQuery, function (error, results) {
if (error) throw error;
const output = results[0].solution;
console.log(output);
});
connection.end();
} catch (e) {
console.error(e.message); // or "throw Error(e.message)", this shows it directly on top of the webpage
}
}
The button I use to trigger the function (without the () =>
Next.js thinks it's an object)
<button onClick={() => executeMySqlQuery("SELECT 1 + 190 / 2 AS solution;")}> Execute SQL Query </button>
I also attempted this
<button onClick={async () => await executeMySqlQuery("SELECT 1 + 190 / 2 AS solution;") } > Execute SQL Query </button>
I encounter the following error:
Uncaught (in promise) Error: Cannot read properties of undefined (reading 'call')
This is the error in the DevTools console: https://i.sstatic.net/infN3.png
This is the error in Next.js, when I use throw Error()
.
https://i.sstatic.net/8m8TZ.png