I am working on a discord bot using discord.js and I can't figure out why my function is always returning undefined. I have one function that reads a JSON File and converts it into an object and that object should then be returned so i can use it in other functions.
function getPlayer(message) {
let filename = message.author.id + '.json';
let path = './Database/';
if(fs.existsSync(path + filename)) {
console.log('File Found');
fs.readFile(path + filename , 'utf-8', (err,content) => {
if(err) {
console.log(err)
} else {
let player = JSON.parse(content);
return player;
}
})
} else {
console.log('File not found')
}}
Inside this method I can access the properties without a problem but as soon as I use the getPlayer() method to get the player object in another method I always get an error that player is not defined.
function getELO (message) {
let p = getPlayer(message);
console.log(p.name) }