The problem is the gotxp
variable is undefined when it needs to be used to define lvl
, which is then sent to the sendimg()
function.
Is there a way to wait until gotxp
is defined?
The gotxp
variable gets defined by a MySQL query which returns a number.
async execute(client, message, args) {
if(!args[0]){
var gotxp = await readdb(client,message,args);
let lvl=(gotxp / 100);
console.log('1 lvl ' + lvl + ' gotxp ' + gotxp);//Just a Debug Thing
let username = message.author.username;
let useravatar = message.author.displayAvatarURL({ format: 'png' });
sendimg(lvl,username,useravatar,message)
}
//here is the readdb function
async function readdb(client,message,args){
var con = mysql.createConnection({
host: client.dbconfig.host,
user: client.dbconfig.user,
password: client.dbconfig.password,
database: client.dbconfig.database,
supportBigNumbers: true,
bigNumberStrings: true
});
if(!message.mentions.users.size){
con.query(`SELECT * FROM userdata WHERE serverid = '${message.guild.id}'`, (err, fields) => {
if (err) {
return message.channel.send(`Error - Problema de conexión con la base de datos, prueba más tarde`);
}
let nowlvl = 0;
if(fields.length >= 1){
let id = message.author.id;
let gotfields = search(fields,id)
console.log(gotfields)
if(typeof gotfields != 'undefined') {
nowlvl = gotfields.xp;
console.log('1 nowlvl ' + nowlvl);
}
else{
nowlvl = 0;
console.log('2 nowlvl ' + nowlvl);
}
}
setTimeout(function() { con.end() }, 100)
return nowlvl;
});
}
else{
con.query(`SELECT * FROM userdata WHERE serverid = '${message.guild.id}'`, (err, fields) => {
if (err) {
return message.channel.send(`Error - Problema de conexión con la base de datos, prueba más tarde`);
}
let nowlvl = 0
if(fields.length >= 1){
var id = message.mentions.users.map(user => {
return `${user.id}`;
});
let gotfields = search(fields,id)
if(typeof gotfields != 'undefined') {
nowlvl = gotfields.xp;
console.log('3 nowlvl ' + nowlvl);
}
else{
nowlvl = 0;
console.log('4 nowlvl ' + nowlvl);
}
}
setTimeout(function() { con.end() }, 100)
return nowlvl;
});
}
}
Read more here: https://stackoverflow.com/questions/67005942/wait-until-some-variable-is-defined-nodejs
Content Attribution
This content was originally published by GaryCraft at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.