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

291
Vistas
Node.js my function showing undefined dictionary value despite assigning it

I'm new to javascript so i'm sorry if this has been asked before.

I have this function which i use to read json files.

const jsonfile = require('jsonfile');

function readData(fileName: string, callback) {
jsonfile.readFile(".\\data\\" + fileName + ".json", 'utf8', function (err, data) {
    if (err) console.error(err)
    callback(null, data);
 })
}

Then I call it using this:

// Read JSON files
var fileList = ["guides"];
var files = {};

for (let i = 0; i < fileList.length; i++) {
    readData(fileList[i], function (err, result) {
        if (err) throw err;
        files[i] = result;
    })
}

but when I:

console.log(files['guides']);

it returns undefined. Can anyone help me fix this? Thank you very much.

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Callback function of the jsonfile.readFile function is called asynchronously and your console.log statement is executed synchronously.

Asynchronous code is executed after the synchronous execution of your javascript code has ended; as a result, your console.log statement is logging files['guides'] before guides property is added in the files object. This is why you get undefined.

Following code example shows this problem in action:

let a = 1;

setTimeout(() => {
  a = 2; 
}, 100);

console.log(a);

Above code snippet outputs 1 because the callback function of setTimeout is invoked asynchronously, i.e. after the console.log(a) statement has been executed and at the time of execution of console.log statement, value of a is 1.

Solution

To make sure that you log files["guides"] after all the files have been read and files object has been populated, you could return a promise from the readData function.

Following code shows how you could create a promise wrapper around readData function:

function readData(fileName) {
   return new Promise((resolve, reject) => {
      jsonfile.readFile(".\\data\\" + fileName + ".json", 'utf8', function (err, data) {
         if (err) reject(err);
         else resolve(data);
      })
   }
};

Now you can call the above function as:

Promise.all(fileList.map(filePath => readData(filePath)))
  .then(dataFromAllFiles => {
     console.log(dataFromAllFiles);
  })
  .catch(error => console.log(error));

Useful Links:

  • Asynchronous JavaScript

  • Graceful asynchronous programming with Promises

  • Using Promises

about 4 years ago · Santiago Gelvez Denunciar
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