0

Trying to query my mySQL database but I can't because i get a TypeError: connection.query is not a function error. Does anyone have any idea why? I don't have any clue as to why this is happening.

database.js

const fs = require('fs'); // node.js file system module
require('dotenv').config(); // stores tokens, keys, passwords and other info
const Discord = require('discord.js'); // links discord.js api to file
const database = require('./database.js');

const client = new Discord.Client(); // creates bot user

let connection;
(async () => {
    connection = await require('./database.js');
    await client.login(process.env.TOKEN); // bot goes from offline to online
})();

client.once('ready', () => console.info(`[${date.toLocaleString()}] INFO | Ready, logged in as ${client.user.tag} (${client.user.id})\n------------------------`));

client.on('guildCreate', async guild => {
    try {
        await connection.query(`INSERT INTO guildInfo VALUES('${guild.id}', '${guild.ownerID}')`);
        await connection.query(`INSERT INTO guildConfig (guildID) VALUES('${guild.id}')`);
    } catch(err) {
        console.error(err);
    }
});

database.js

require('dotenv').config();
const mysql = require('mysql2/promise');

date = new Date();
mysql.createConnection({
    user: process.env.USER,
    password: process.env.PASSWORD,
    database: process.env.DATABASE
}).then(connection => console.info(`[${date.toLocaleString()}] INFO | Waiting for input/changes to code\n------------------------`)).catch(err => console.error(err));

Error

TypeError: connection.query is not a function
    at Client.<anonymous> (C:\Users\Patrick Lawrence\Desktop\Synth\index.js:43:20)
    at Client.emit (events.js:315:20)
    at Object.module.exports [as GUILD_CREATE] (C:\Users\Patrick Lawrence\Desktop\Synth\node_modules\discord.js\src\client\websocket\handlers\GUILD_CREATE.js:33:14)
    at WebSocketManager.handlePacket (C:\Users\Patrick Lawrence\Desktop\Synth\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\Patrick Lawrence\Desktop\Synth\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\Patrick Lawrence\Desktop\Synth\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\Patrick Lawrence\Desktop\Synth\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\Patrick Lawrence\Desktop\Synth\node_modules\ws\lib\websocket.js:797:20)
    at Receiver.emit (events.js:315:20)
2
  • 1
    database.js is not exporting anything, it seems. Commented Sep 1, 2020 at 22:29
  • @aioros thanks! how would I export it? Commented Sep 1, 2020 at 22:33

1 Answer 1

1

Simply you don't export the connection in database.js.

Once created connection, assign it to a variable like const connection and type at bottom of the file module.exports = connection.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.