0

When querying a DB in TS using node-sqlite3 the query happens after everything else.

The docs haven't yielded any answers, though there may be something glaringly obvious that I've missed.

I've attempted both of the following

const sqlite3 = require('sqlite3').verbose()
const dexDB = new sqlite3.Database('./sql/pokedexes.sqlite')
const monDB = new sqlite3.Database('./sql/mons.sqlite')

async function getDex(row: number): Promise<object>{
    dexDB.serialize(()=>{
        dexDB.get(`SELECT pokedex FROM pokedexes WHERE rowid = ${row}`, (err, row)=>{
            const dexObj = JSON.parse(row.pokedex)
            console.log(dexObj.pokeapiDexName)
            return dexObj         
        })
    })
}

const tempDex = await getDex(1)
console.log(tempDex.pokeapiDexName)

dexDB.close()
monDB.close()

export {}
function getDex(row: number): object{
    dexDB.serialize(()=>{
        dexDB.get(`SELECT pokedex FROM pokedexes WHERE rowid = ${row}`, (err, row)=>{
            const dexObj = JSON.parse(row.pokedex)
            console.log(dexObj.pokeapiDexName)
            return dexObj         
        })
    })
}

const tempDex = getDex(1)
console.log(tempDex.pokeapiDexName)

dexDB.close()
monDB.close()

export {}

both of which return the same output of

undefined
kanto

though I was expecting it to have output

kanto
kanto

In addition, I attempted to wrap the call in a Promise as seen here

const sqlite3 = require('sqlite3').verbose()
const dexDB = new sqlite3.Database('./sql/pokedexes.sqlite')
const monDB = new sqlite3.Database('./sql/mons.sqlite')

async function getDex(row: number): Promise<object>{
    return new Promise((resolve, reject)=>{
        let dexObj = {}
        dexDB.serialize(()=>{
            dexDB.get(`SELECT pokedex FROM pokedexes WHERE rowid = ${row}`, (err, row)=>{
                if(err){reject(err)}
                dexObj = JSON.parse(row.pokedex)
                console.log("inside func " + dexObj.pokeapiDexName);
                ()=>{resolve(dexObj)}
            })
        })
    })
}

console.log("before func call")
const tempDex = await getDex(1)
console.log("after func call")
console.log(tempDex)

dexDB.close()
monDB.close()

export {}

which returns

before func call
inside func kanto

as well as an indicator in vscode's terminal stating Command executed now and failed (Exit Code 1)

When I move ()=>{resolve(dexObj)} outside as a tertiary parameter of dexDB.get() I no longer get Command executed and failed, though it returns to running the query at the end and outputs

before func call
after func call
{}

1 Answer 1

0

The answer here provided a solution

const dexDB = new sqlite3.Database('./sql/pokedexes.sqlite')

must be called within the function itself, not at the beginning of the file. Unsure as to why exactly, but it works now and properly outputs

before func call
after func call 
kanto
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.