I'm trying to acquire information from a mysql database in nextjs13 using the mysql2 dependency, like this:
import mysql from 'mysql2/promise'
export const getData = async(req, res) => {
const dbconnection = await mysql.createConnection({
host: "hostname",
database: "databasename",
user: "username",
password: "password",
});
try {
const query = "SELECT productid, productname, productimage FROM products";
const values = [];
const [data] = await dbconnection.execute(query, values);
dbconnection.end();
res.status(200).json({ products: data });
} catch (error) {
// unhide to check error
// res.status(500).json({ error: error.message });
}
return dbconnection
}
At first the error was that the net module was missing, which is called when mysql.createConnection is used. Then when I added the module, it said that net.connection is not a function. I have already tried it in both client side and server side components and neither work. The strings in the object are examples ofc. Does anybody know what could it be?
Via Active questions tagged javascript - Stack Overflow https://ift.tt/tWLqp9V
Comments
Post a Comment