I'm trying to get a function to return a boolean value to that which called it and no matter what I try I cannot seem to get it to work (this is running in a script that is required by a main script in another file), here is my current code:
const { Client } = require('pg')
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: true,
});
async function checkschema(name) {
try {
client.connect()
client.query(`SELECT EXISTS(SELECT schema_name FROM information_schema.schemata WHERE schema_name = '${name}');`, (err, res) => {
if (err) throw err;
let result = JSON.stringify(res.rows[0]["exists"])
console.log("result is: "+result) // logs "result is: true/false"
client.end()
return result
})
} catch(err) {
console.log("error: "+JSON.stringify(err))
}
}
module.exports.getdata = (serverid, userid) => {
checkschema(serverid).then((result) => {
console.log("result 2 is: "+result) // logs "result 2 is: Undefined"
return result;
})
}
What confuses me is when I run something like this:
async function check() {
let result = true
console.log("result 1 is: "+result) // result is: true
return result
}
check().then((result) => {
console.log("result 2 is: "+result) // result is: true
})
everything works.