I have a "data" variable that is in the global scope of the function and I am updating that inside the inner function to read a file.
function timeSeries(obj) {
data = [
{
original_value: [],
forecasted_value: [],
min_band: [],
max_band: [],
line_status: [],
timestamp: []
}
]
fs.readFile("./api/assignment_data/" + obj._id + ".json", "utf-8", function read(err, datas) {
if (err) {
throw err;
}
const filedata = JSON.parse(datas)
data[0].original_value.push(filedata.map(line => line.original_value))
data[0].forecasted_value.push(filedata.map(line => line.forecasted_value))
data[0].min_band.push(filedata.map(line => line.min_band))
data[0].max_band.push(filedata.map(line => line.max_band))
data[0].line_status.push(filedata.map(line => line.line_status))
data[0].timestamp.push(filedata.map(line => line.timestamp))
console.log(data[0].original_value[0][0])
})
return data;
}
when I console.log in place of return data or look into variable the function returns value to..it is empty the way it is declared above. however, if I console.log inside the file read function it prints successfully