Issues exporting node.js mysql connection object

26 views Asked by At

I'm making a discord.js bot that connects to a mysql database and I'm using the mysql2 node.js package.

There's two relevant files query.js and index.js

Index.js:

//...more code above
var mysql      = require('mysql2');
const { waitForDebugger } = require('node:inspector');
var connection = mysql.createConnection({
  host     : '127.0.0.1',
  user     : 'root',
  password : process.env.dbPass,
  database : 'ucbot_db'
});
 
connection.connect();

//this part is just to test if the connection works, which it does
connection.query('SELECT * from clans', function (error, results, fields) {
    if (error) {
        console.log(`Error: ${error}`);
    }
    console.log(`${JSON.stringify(results)}`);
});

module.exports = {connection};

Query.js

const { SlashCommandBuilder } = require('@discordjs/builders');
const { connection } = require('../index.js');

console.log(connection);

connection is undefined and I cannot figure out why. The file structure is like this

Root folder
|-commands
|-|-query.js
|
|-index.js
0

There are 0 answers