简体   繁体   中英

Creating array of JSON objects from file

i have a list of JSOn files, which i want to read into an array, so each object is the content of the json file.

I'm new to Node and Express, but i have tried this so far.

const express = require('express')
const app = express()
const port = 3001
const fs = require('fs')


//file upload 


const dataFolder = './parsed_json_data' 


let fileNames = []
let fileObjects = []
fs.readdir(dataFolder,(err, files) =>{  
    if(err){
    return console.log(err)
    }   
    fileNames = files.forEach(file =>{
    fileNames.push(file)
    console.log(fileNames)
    });
})

fileNames.forEach(fileName =>{
    fs.readFile(fileName, 'uft8', (err, data) =>{
        if(err){
            console.log(err)
        }
        fileObjects.push(JSON.parse(data))
        console.log(fileObjects)
    })
})




app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

Is the issue, that it is an async operation? If this is the case, how can i make it wait, for the filenames to be read in before mapping over?

I have tried with async/await, but with no luck, since it is to seperate operations?

I think the code iterates through the fileNames array before the file system calls finish. You may be able to try to put the code to create the fileNames in its own function that returns a promise of the array. Please see code below:

function getDirFiles(dataFolder) {
  return new Promise((resolve, reject) => {
    let fileNames = [];
    fs.readdir(dataFolder, async (err, files) => {
      if (err) {
        console.log(err);
        reject(err);
      } else {

        files.forEach(file => {
          fileNames.push(file);
          console.log(fileNames);
        });

        resolve(fileNames);

      }
    });
  });
}

getDirFiles(dataFolder).then(fileNames => {
   console.log(fileNames);
});

You can use the array you are mapping over within a forEach. Calculate the total length of the array and fire the function once the last fakeItem has been reached.

const fakeItems = [...document.querySelectorAll('.items)]
fakeitems.forEach((fakeItem, i, arr) => {
   const arrLength = arr.length - 1
   if (i === arrLength) {
       secondFunction()
    }
})
function secondFunction() {
 /* This function fires once the last item is mapped though */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM