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?