I'm in a project using NodeJS, and its Express framework.
In the main file app.js I call a function to read a file, and assign it to a global variable:
global.google_sheet_credentials = readFileCredentials("spread_sheet.txt");
The function code readFileCredentials is:
readFileCredentials = function(file){
fs.readFile('data-source/credentials/'+file, 'utf8', function(err, data){
if(err){
console.log("Could not open file: %s", err);
}else{
console.log(data);
return data;
}
});
};
module.exports = readFileCredentials;
After that, I redirect to another file:
rek('dashboards/ti/main_it');
In this file main_it.js , I try to use this global variable google_sheet_credentials :
console.log(google_sheet_credentials);
However, it warns that it is undefined, can someone explain the reason for it, and a way for me to get the right result?