1

I have a folder filled with json files and I am trying to push them all into an array. However, the outer array is blank. Any help on why this is happening or explaining a better approach would be appreciated.

var fs = require('fs');
var dataFolder = './jsonData/';

var arr = [];
fs.readdir(dataFolder, (err, files) => {
    files.forEach(file => {
        fs.readFile(dataFolder + file, 'utf8', function (err, data) {
            if (err) throw err;
            arr.push(data);
            console.log (arr); // correct output
        })
    })
})
console.log (arr);  //output is []

Sorry for how ugly it looks, but here's some of the output from the inner array:

[ '{\n "title": "A New Kind of Science [Hardcover]",\n "author": "Stephen Wolfram",\n "price": "$20.73",\n "shippingWeight": "5.6 pounds",\n "isbn10": "1579550088"\n}' ]

[ '{\n "title": "A New Kind of Science [Hardcover]",\n "author": "Stephen Wolfram",\n "price": "$20.73",\n "shippingWeight": "5.6 pounds",\n "isbn10": "1579550088"\n}', '{\n "title": "I Wear the Black Hat: Grappling with Villains (Real and Imagined) [Hardcover]",\n "author": "Chuck Klosterman",\n "price": "$15.49",\n "shippingWeight": "2.2 pounds",\n "isbn10": "1439184496"\n}' ]

1 Answers1

2

readdir is asynchronous it has not executed by the time you get to your console.log.

  1. var arr = []; - set the arr to an empty array.
  2. fs.readdir(....) go read the directory in the back ground and schedule the callback to execute after readdir background job is done and this file has finished excuting.
  3. console.log(arr); print arr as it is now (empty).
  4. readdir is done and there is nothing else left on the call stack so now we can finally execute the callback.
  5. start pushing to arr.
  6. console.log(arr); print arr as it is now (finally populated).

Also global state is a source of much confusion and bugs. Try to design your code so it does not rely on global state. (https://softwareengineering.stackexchange.com/a/148109/16887)

Community
  • 1
  • 1
Sukima
  • 9,965
  • 3
  • 46
  • 60