4
I am in a project using Nodejs, and its Express framework.
In the main archive app js. i call a function in order to read a file, and assign to a global variable:
global.google_sheet_credentials = readFileCredentials("spread_sheet.txt");
The code of the function readFileCredentialls 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);
but warns that it is undefined, someone can explain to me the reason, and a way for me to get the right result?
You can create a module to read the
.txt
and import it into the file you want.– Lucas Fontes Gaspareto
That one
readFileCredentials
is synchronous?– Sergio
It is a function that takes the name of the file, and returns the contents of that file, I can’t tell you if it is synchronous or asynchronous.
– Jonathan
You can put the code of that function here?
– Sergio
I put the code in the question.
– Jonathan