0

This question has been asked before but I still don't get the understanding Set value of variable in the if block. Access value outside if block In my case I decleared a variable outside the if block var title, artist, album, year but when I accessed it outside the if block filedata.push({fileName: file.name, location: path, title: title, artist: artist, album: album, year: year}) it says undefined

function selectMusic() {
  const fileList = songs.files
  for (let i=0; i < fileList.length; i++) {
    let file = fileList.item(i)
    var path = (window.URL || window.webkitURL).createObjectURL(file)
    var reader = new FileReader();
    var title, artist, album, year
    reader.onload = function(e) {
      var dv = new jDataView(this.result);
      // "TAG" starts at byte -128 from EOF.
      if (dv.getString(3, dv.byteLength - 128) == 'TAG') {
        title = dv.getString(30, dv.tell());
        artist = dv.getString(30, dv.tell());
        album = dv.getString(30, dv.tell());
        year = dv.getString(4, dv.tell());
      } else {
        console.log('id3 metadata not found')
      }
    };
    reader.readAsArrayBuffer(file);
    filedata.push({fileName: file.name, location: path, title: title, 
    artist: artist, album: album, year: year})
    //console.log(file.name)
    console.log(filedata)
  }
  //const arr = Array.from(fileList)
  // Load song initially
  loadSong(filedata[songIndex]);
}
code46
  • 95
  • 2
  • 7
  • where are you declaring filedata? I see that you try see the value of fileDate but I cannot see where it is declared – Mykyta Halchenko Apr 15 '22 at 13:42
  • @Skalpel02 it may be defined outside of the function – Archit Gargi Apr 15 '22 at 13:44
  • @ArchitGargi it may be, but in that case there must be the value or error. – Mykyta Halchenko Apr 15 '22 at 13:45
  • The problem is not the `if` block, the problem is that you declared the variable outside of the `onload` function, and attempt to read the values before the filereader has called the function. – Bergi Apr 15 '22 at 14:12
  • @Skalpel02 It is declared outside the function because the track information is used to load the music – code46 Apr 15 '22 at 21:35
  • @Bergi So what can I do to solve this. I understand you explanation very well – code46 Apr 15 '22 at 21:38
  • Move the `filedata[i] = {fileName: file.name, location: path, title: title, artist: artist, album: album, year: year};` inside the load callback. Also don't call `loadSong(filedata[songIndex]);` right after the loop (which has only started to read all the files), but rather put an `if (i == songIndex) loadSong(filedata[songIndex]);` in the callback as well, so that it starts only after that song file has been read. – Bergi Apr 15 '22 at 21:55
  • @Bergi Its working now and I couldn't write the call back function so I noticed that the track information for the song is not loaded immediately unless you skip to the next song please give me an idea on how to write a call back for my case – code46 Apr 16 '22 at 11:52
  • @Bergi Thanks a lot everything works... Massive gratitude.. – code46 Apr 18 '22 at 18:34

0 Answers0