Today I was trying to get some data from mysql with a function, and when i console.log the values inside the function, all returns properly, but if i return <value>, it returns undefined (rows[0] is an object), for example:
function getData(userID, guildID) {
connection.connect();
this.guildID = guildID ?? null
this.userID = userID ?? null
connection.query(`SELECT * FROM data_${this.guildID} WHERE id = '${this.userID}'`, (err, rows, fields) => {
if(err) throw err;
connection.end()
this.data = rows[0]
return this.data;
})
}
returns undefined when i console.log(getData(etc, etc))
but if i do:
function getData(userID, guildID) {
connection.connect();
this.guildID = guildID ?? null
this.userID = userID ?? null
connection.query(`SELECT * FROM data_${this.guildID} WHERE id = '${this.userID}'`, (err, rows, fields) => {
if(err) throw err;
connection.end()
this.data = rows[0]
console.log(this.data);
})
}
it works perfectly fine, which logs:
RowDataPacket {
id: '573991320897716224',
bank: '0',
wallet: '0',
unlimited: '0'
}