Looking to create an array from a file, the following code snippet serves the purpose:
'use strict';
const fs = require('fs');
let results = [];
fs.readFile('myfile.json', (err, data) => {
if (err) throw err;
results = JSON.parse(data);
//console.log(results); This works fine
});
console.log('results length:', results.length);
for ( const r in results) {
console.log('res', r);
console.log(r.configuration.value);
}
The JSON object appears in the console as expected.
However, attempting to access outside of fs.readFile
displays:
results length: 0
Furthermore, the loop fails to iterate.
Seeking guidance on resolving this issue.