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?
#db.run is not a function (better-sqlite3)
73 messages · Page 1 of 1 (latest)
- 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
Show code
(the file where it says the error is coming from)
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);
},
};```
What is db?
const db = require('better-sqlite3')('databes.db');
did you module.exports it
yes
just a bit below
Did you export it as a default? You're importing it as one
const db = require('../index') looks like you have a circular dependency
what's in ../index?
basically this single line
const db = require('better-sqlite3')('databes.db');
and the export
module.exports = client, db;
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)
sure
As Svitkona said, you can't do that, make module.exports an object with those instead
Log it where you import it
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)
I would also suggest you brush up on your JS, especially Node-specific stuff as well
uhhhh, it's somehow logging discord api related things
It's fine now, we've sorted out your issue
But do change this
.
issue is
db.exec doesnt work either
same error?
let me check it one more time tho
yup, as far as i remember

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)
It is
yes i know
that's what the console logs though
Where are you logging it? In your index.js or in your command file where you import it from your index.js?
logged both places, index.js where i've done the import logged this:
and where my event listener is it logged discord api datas
somehow
You're not logging the right thing then.
Fix this and try again
right, how do i need to fix it, again?
Make it an object and put the stuff in that object
aight
so im basically doing this, correct?
module.exports = {client, db};
Yes
right
I've done it
It's now throwing an unrelated error to this topic
guess because the export is an object
Show the error
client.on(Events.MessageCreate, (message) => {
^
TypeError: client.on is not a function
What is client
it's
client
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
module.exports = {client, db};
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
the message listener and the on ready code are in separate files though, how could i do that?
still...the same way?
show me how you're importing client in the ready file
right, i think i got it