I want to make sure the database opens properly, then execute the get statement, then output it. But I am the following in the console where it's getting executed simultaneously. Thanks in advance!
Console log:
here1
here2
usergp is undefined
usergp in get is 10931
Desired output:
here1
usergp in get is 10931
here2
usergp is 10931
Code:
let selectGP = 'select gp from Money where userID = ? and guildID = ?';
let userGP;
let db;
const openDB = new Promise(resolve => {
db = new sqlite3.Database('./Toothless.db', (err) => {
if (err) {
console.error(err.message);
}
});
resolve();
});
x = openDB.then(() => {
console.log('here1');
db.get(selectGP, [interactionUserID, interactionGuildID], (err, row) => {
if (err) {
console.log('in test > select GP');
} else {
userGP = row.gp;
console.log('usergp in get is ' + userGP);
return userGP;
}
});
}).then((data) => {
console.log('here2');
console.log('usergp is '+ data);
interaction.reply({
content: 'you have ' + data
, ephemeral: true
});
db.close();
}).catch(() => {
console.log('in catch');
});