[SOLVED] Export value from SQLite query to another js file

salm2s

Honorable
Jul 21, 2017
266
6
10,815
Hello there,
I am trying to make a discord bot that uses SQLite that stores user id. In order to keep code clean, I have made a file called db.js where I store all my db functions
JavaScript:
const sql = require('sqlite3')
let db = new sql.Database('./userdb.db', sql.OPEN_READWRITE, (err) => {
    if (err) console.error(err.message)
})
module.exports =  {
    addUser: function(id) {
        db.run(`INSERT INTO user(id) VALUES(?)`, [id], (err) => {
            if (err) console.error(err.message)
            console.log("User added")
        })
    },
    removeUser: function(id) {
        db.run(`DELETE FROM user WHERE id = ${id}`)
    },
    checkUser: function (userid) {
        var ids = undefined;
        db.all(`SELECT id FROM user WHERE id = ${userid}`, (err, rows) => {
            if (err) console.log(err.message)
            if (rows[0].id != undefined) {
                ids = rows[0].id
            }
            else {
                return
            }
        })
        return ids
    }
}

Mainly focusing on checkUser function. I am trying to make it so that it will return the ID if the user is found in the database. I am using it in my main bot file like so:

JavaScript:
const query = require('./db')
....
if (query.checkUser(member.user.id) == undefined){
    // Add the role
    member.roles.add(role)
    // And add them to database
    query.addUser(member.user.id)
}
When the user registers first time, it works and add's them to database. However, when they register again, it still add's them to database. So I tried to console.log(query.checkUser(member.user.id)) and it kept returning undefined.

How can I solve this issue?

Thanks,
salm2s
 
Solution
I found a solution: USE better-sqlite3!!!

JavaScript:
const sql = require('better-sqlite3')
...
checkUser: function (userid) {
        const db = new sql('./userdb.db', {readonly: true})
        const row = db.prepare("SELECT id FROM user WHERE id = ?")
        const id = row.get(userid)

        if (id == undefined) {
            return true
        }
        else {
            return false
        }
    },

Ralston18

Titan
Moderator
The object being to not allow the user to be entered into the database more that once - correct?

var ids is = undefined.

Wondering about those two sequential "if" statements in the checkUser function

Take a closer look at your "if user already exists in the database then return "User already registered" logic.

And redefines var ids = User already registered

Logic that tests for the existence of the user in the database and then results in something like:

if (err) console.error(err.message)
console.log("User already registered")
 

salm2s

Honorable
Jul 21, 2017
266
6
10,815
I found a solution: USE better-sqlite3!!!

JavaScript:
const sql = require('better-sqlite3')
...
checkUser: function (userid) {
        const db = new sql('./userdb.db', {readonly: true})
        const row = db.prepare("SELECT id FROM user WHERE id = ?")
        const id = row.get(userid)

        if (id == undefined) {
            return true
        }
        else {
            return false
        }
    },
 
Solution

Ralston18

Titan
Moderator
Curious:

What led you to the "better-sqlite3" solution?

Was it truly the use of "better-sqlite3" or was it the rewrite of function checkUser?

If you use the rewritten checkUser function with the originally coded "require ( 'sqlite3')" as presented in Post #1 = what happens?

What error(s) or results occur?

Also FYI: Forum rules prohibit self-awarding of Best Answers.

Please be aware that your self-awarded Best Answer may be removed accordingly.