Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
global variable is returns empty in node.js

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!