I’m working on a Discord bot with Javascript, and I’ve been trying to implement a new command.
However, I can’t get one function to work properly.
The function here is used to get a value from a .json file and return the value as a string, however, it doesn’t work.
function readmood(moodvaleur) {
fs.readFile(moodpath, 'utf8', (err, jsonString) => {
if (err) {
console.log('Error reading the JSON file:', err);
return;
}
try {
const moodlist2 = JSON.parse(jsonString);
console.log(moodlist2);
console.log(moodvaleur);
console.log(moodlist2[moodvaleur]);
const final = moodlist2[moodvaleur];
return final;
}
catch (err) {
console.log('Error parsing JSON string:', err);
}
});
}
I don’t know what could be wrong since the console.logs inside the function show me that I’m able to retrieve the json list, and the value that I want (and it tells me that it is a string too), but once I try to use the function for real (console.log(readmood(value)), it’s just “undefined”.
If someone could please help me understand what I’m missing here, I would be very grateful !