I would like to console.log a message and return an empty array if I try to read a file that doesn't exist. The getAllNotes function fires effective and the error is definitely thrown no such file or directory, open 'notes.json'.
However, why is the error thrown if I didn't specify throw err in my if statement? Secondly why doesn't my console.log message get outputted and the empty array not returned?
var getAllNotes = () => {
console.log("MADE IT TO GET ALL NOTES")
var notesArray = fs.readFileSync('notes.json', (err, data) => {
if (err) {
console.log("There are no notes to display");
return [];
} else {
console.log("DATA",data)
console.log("MADE IT TO PARSE")
return JSON.parse(data);
}
});
console.log("INSIDE GET ALL NOTES", notesArray)
};