I am trying to store the data read by fs in a variable. But I am getting undefined as output.
const fs = require("fs");
var a;
fs.readFile("analogData.txt", "utf8", (err, data) => {
a = data;
});
console.log(a);
I am trying to store the data read by fs in a variable. But I am getting undefined as output.
const fs = require("fs");
var a;
fs.readFile("analogData.txt", "utf8", (err, data) => {
a = data;
});
console.log(a);
const fs = require("fs");
let a;
fs.readFile("analogData.txt", "utf8", (err, data) => {
a = data;
console.log(a);
});
You might have not created the text file. SO, create it and run the program again.
For console.log outside of the callback function, it always gives undefined. Since it executes before "a" variable being set in the callback. Callback function runs after console.log.