const fs = require('fs');
var students = [];
var programs = [];
module.exports.initialize = function(){
return new Promise((resolve, reject) => {
fs.readFile('./data/students.json', 'utf8',(err,data)=>{
if(err) reject("Unable to read student data");
students = JSON.parse(data);
});
console.log(students);
fs.readFile('./data/programs.json', 'utf8',(err,data)=>{
if(err) reject("Unable to read programs data");
programs = JSON.parse(data);
});
console.log(programs);
resolve("Both data read successfully");
})
}
I don't know why this is happening but when the fs.readFile() function is done, both students and programs become empty. They are not empty when console.log() after JSON.parse() inside the readFile() function.