I'm using node's readdir to asynchronously read the content of a directory then parse it with music-metadata. From the metadata, I want to extract artist and title info, and return it as json. Here's my code:
router.get('/library/', (req, res) => {
const collection = process.cwd() + '/music/'
let library = {
songs: [],
}
fs.readdir(collection, (err, files) => {
if (err) throw err
files.forEach(async file => {
let song = {
artist: '',
title: '',
}
let resopnse = await mm.parseFile(collection + file)
song.artist = resopnse.common.albumartist
song.title = resopnse.common.title
library.songs.push(song)
})
console.log(library) // why this is empty
})
res.json({ message: `${library}` })
})
The library object returned by the above is empty. I thought that I'm awaiting in all the right places? What did I miss?