#db.run is not a function (better-sqlite3)

73 messages · Page 1 of 1 (latest)

open dust
#

Hiya! I'm extremely new to js, am a python dude mainly. I started working on a project with my friend who knows js better but we couldn't figure out what's the issue. We're trying to do basic database stuff but for some reason the bot doesn't even want to start. It logs db.run is not defined as far as i can tell from my personal experience and from online resources everything is just fine. Would someone mind helping me?

cyan arrowBOT
#
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
  • Marked as resolved by OP
fallow flint
#

Show code

#

(the file where it says the error is coming from)

open dust
#
const { Events } = require('discord.js');
const db = require('../index')

module.exports = {
    name: Events.ClientReady,
    once: true,
    execute(client) {
        console.log(`Ready! Logged in as ${client.user.tag}`)
        const createIfNotExists = ('CREATE TABLE IF NOT EXISTS users (id INTEGER');
        db.run(createIfNotExists);
    },
};```
fallow flint
#

What is db?

open dust
#
const db = require('better-sqlite3')('databes.db');

limpid moon
#

did you module.exports it

open dust
#

yes

#

just a bit below

fallow flint
#

Did you export it as a default? You're importing it as one

vital lynx
#

const db = require('../index') looks like you have a circular dependency

#

what's in ../index?

open dust
#

basically this single line

#

const db = require('better-sqlite3')('databes.db');

#

and the export

#

module.exports = client, db;

vital lynx
#

can you log db after initialising it
(btw, you can't export multiple things like that, though that isn't the source of your issue)

fallow flint
fallow flint
#

Log it where you import it

vital lynx
#

ok i just checked the API and it seems like db.run isn't a thing
you can use db.exec instead (which isn't really recommended. use prepared statements instead)

fallow flint
#

I would also suggest you brush up on your JS, especially Node-specific stuff as well

open dust
fallow flint
fallow flint
open dust
#

issue is

#

db.exec doesnt work either

vital lynx
#

same error?

open dust
#

let me check it one more time tho

open dust
open dust
#

oh good, cant even reach db.exec

#

db.prepare isn't a function

#

its not not defined, sorry

#

used to python lol

#

i dont know if that changes on anything

#

db.run is not a function (better-sqlite3)

fallow flint
open dust
#

yes i know

#

that's what the console logs though

fallow flint
#

Where are you logging it? In your index.js or in your command file where you import it from your index.js?

open dust
#

and where my event listener is it logged discord api datas

#

somehow

fallow flint
#

You're not logging the right thing then.

fallow flint
open dust
open dust
fallow flint
open dust
#

aight

#

so im basically doing this, correct?

#

module.exports = {client, db};

fallow flint
#

Yes

open dust
#

right

#

I've done it

#

It's now throwing an unrelated error to this topic

#

guess because the export is an object

fallow flint
#

Show the error

open dust
#

client.on(Events.MessageCreate, (message) => {
^

TypeError: client.on is not a function

fallow flint
#

What is client

open dust
#

it's

#

client

#

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});

module.exports = {client, db};

jagged acorn
#

you're exporting an object which contains client and db, so to import you would destruct or use the object directly

const { client, db } = require('../index');

// or

const imports = require('../index');

imports.client;
imports.db;
#

the former is a better option

open dust
jagged acorn
#

still...the same way?

#

show me how you're importing client in the ready file

open dust
#

right, i think i got it