0

I m not super familiar with promises, callbacks,etc...

I'm trying to read a directory with fs.readdir, then I loop into each files and check if it exists in the Db, if not, I would like to return a object with the filename.

I try different ways, but right now I make this fuction. I manage to console.log the files and the new video into the loop, But i never managed to get it outside of the function.

function getNewFile(){
  let video = {}
  fs.promises.readdir('public/video').then(files=>{
    files.forEach( async file => {
      try{
        let id = await Video.exists({filepath: `/video/${file}`})
        if(id==null) {
          video = {
            "name":file,
            "filepath":'/video/'+file,
          }
          return video
        }
        
      }
      catch(err){
        console.error('Error occurred while fetching new video!', err);
      }
    })
    
  })
}

I tried with fs.readdir, ad fs.readdirSync.

Fabien
  • 1

0 Answers0