Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda