Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda