I´m using a function to grab data from a database and store those into a array. This array should now be returned to the function which calls it. But the array seems to return undefined even tho it isnt.
Example to call the function (shows undefined):
const data = methods.getPunishmentData();
console.log(data);
Function to return the array:
methods.getPunishmentData = function() {
try {
mysql.executeQuery(`SELECT * FROM log_punishments LIMIT 50`, function (err, rows, fields) {
let punishments_data = [];
rows.forEach(row => {
const playerPunishment = {
time: row['time'].toString(),
admin: {
name: user.getRpName(user.getPlayerById(methods.parseInt(row['admin']))),
id: methods.parseInt(row['admin']),
},
type: row['type'],
player: {
name: user.getRpName(user.getPlayerById(methods.parseInt(row['player']))),
id: methods.parseInt(row['player']),
},
reason: row['reason']
}
punishments_data.push(playerPunishment);
});
return punishments_data;
});
}
catch(ex) {
methods.error('DB Get Punishments', ex);
}
};
What could cause this issue? I would appreciate any kind of suggestions.
I was expecting the code to return a array, which has the following structure:
[
{
time: 'Thu Apr 13 2023 23:42:47 GMT+0200 (GMT+02:00)',
admin: { name: 'Danuh Sus', id: 105 },
type: 'test',
player: { name: 'Danuh Sus', id: 105 },
reason: 'test2'
},
{
time: 'Thu Apr 13 2023 23:42:48 GMT+0200 (GMT+02:00)',
admin: { name: 'Danuh Sus', id: 105 },
type: 'test',
player: { name: 'Danuh Sus', id: 105 },
reason: 'test2'
},
{
time: 'Thu Apr 13 2023 23:42:49 GMT+0200 (GMT+02:00)',
admin: { name: 'Danuh Sus', id: 105 },
type: 'test',
player: { name: 'Danuh Sus', id: 105 },
reason: 'test2'
},
{
time: 'Fri Apr 14 2023 00:02:53 GMT+0200 (GMT+02:00)',
admin: { name: 'Danuh Sus', id: 105 },
type: 'test',
player: { name: 'Danuh Sus', id: 105 },
reason: 'test2'
}
]
This shows that the code itself works and just returns it wrong. To display this I changed return punishments_data; to console.log(punishments_data);