0

I have an issue that I don't understand and I have no idea how to fix it, where to look for the cause. When I'm debugin (chrome) every thing is working, but during normal use it dosen't. For me is some kind of Science-Fiction, it would be better for me if it's more Science than Fiction :)

for (var i = 0; i < filteredAddedFiles.length; i++) {
  if ((/\.(png|jpeg|jpg|gif)$/i).test(filteredAddedFiles[i].name)) {

    (function (file) {
      var reader = new FileReader();
      var blob = b64toBlob(file.base64.replace('data:image/jpeg;base64,', ''), file.type);
      reader.addEventListener("load", function () {

        console.log(this);
        var image = new Image();
        image.src = window.URL.createObjectURL(blob);
        image.onload = function () {
          preview.innerHTML += drawHtml(image, file)
        };

        //I tried:
        //(function (b) {
        //  var image = new Image();
        //  image.addEventListener("load", function () {
        //    preview.innerHTML += drawHtml(this, file);
        //    //window.URL.revokeObjectURL(image.src);
        //  });
        //  image.src = window.URL.createObjectURL(b);
        //})(blob);
      });
      reader.readAsDataURL(blob); 
    })(filteredAddedFiles[i]);

  } else {
    errors += filteredAddedFiles[i].name + " Unsupported Image extension\n";
  }
}

here I attached a short movie that shows how its working link to movie

not working - I mean - it looks like the all thing inside for dosen't executed

EDIT: 1

@Teemu - I turn on logs in chrome console and all console.log's appear

@Katie.Sun - now the above for - console.log(filteredAddedFiles.length); is 0 - but when I'm debuging code the same console.log(filteredAddedFiles.length); have values !

EDIT: 2

@Matti Price filteredAddedFiles - is the result of custom logic of page, filtering, validation etc. Everything starts here:

addedFiles = added(files); // files - FileList from input this is a read only array of obj

function added(from) {
            var out = [];
            for (var i = 0; i < from.length; i++) {
                (function (obj) {
                    var readerBase64 = new FileReader();
                    readerBase64.addEventListener("load", function () {
                        var fileBase64 = readerBase64.result;
                        var row = { name: obj.name, size: obj.size, type: obj.type, base64: fileBase64 }
                        out.push(row);
                    });
                    readerBase64.readAsDataURL(obj); 
                })(from[i]);
            }
            return out;
        }

then addedFiles - do something farther and transform into filteredAddedFiles later. what I found in added function? during debug there is an length value witch is correct, but when I opened the __proto__: Array(0) I found length property = 0.

Should this length value be equal to the value from above length?

print screen

The second thing: I have to admit that I don't have enough knowledge aboute addEventListener. Are there any queues here or some thread etc?

EDIT: 3 After last @Teemu comment I made some changes (I had to read a lot aboute promisses etc:)), but output is the same console.log("out resolve", out); shows a array of object, but console.log("out.length then", out.length); shows 0 and the small blue i-icon show msg - Value below evaluated just now

var out = [];
for (var i = 0; i < files.length; i++) {
    fillArray(files[i], out);
}
console.log("out resolve", out);
console.log("out.length then", out.length);

function fillArray(obj, out) {
    return new Promise(function (resolve, reject) {
        var readerBase64 = new FileReader();
        readerBase64.addEventListener("load", function () {
            var fileBase64 = readerBase64.result;
            var row = { name: obj.name, size: obj.size, type: obj.type, out.push(row);
            resolve(out);
        });
        readerBase64.readAsDataURL(obj); 
    });
}

After I posted the edit above I relized that I just create promise, I forgot to call `promise

, but this is not important, because 90% of my code has been changed because of this topic and the answer of the Golden Person @Kaiido

URL.createObjectURL() - is synchronous. You don't need your Promise wrapping event handlers hell, all can be done in a single loop.

In my case, this is a better solution than a filereader, I have to upload only images, and with some restrictions thanks to which I don't have to worry about freeze the Internet browser, because of the synchronous nature of createObjectURL() Thank you for your help and commitment

szkut
  • 353
  • 2
  • 22
  • You should write down what is not working. Are there any errors in the console? – Andy Nov 08 '18 at 19:16
  • not working - it looks like the all thing inside `for` dosen't executed – szkut Nov 08 '18 at 19:18
  • Why do you say that? – Taplar Nov 08 '18 at 19:19
  • 1
    Then the first thing to do is to check the value of `filteredAddedFiles.length` before the loop. – Teemu Nov 08 '18 at 19:23
  • `console.log(this)` do nothing in console, right know for testing I added `console.log(blob)` below the`'console.log(this)` and it's the same - nothing heppend + function `drawHtml` dosent render anything – szkut Nov 08 '18 at 19:23
  • I added `console.log(filteredAddedFiles.length)` below 'for' and nothing shows up in console – szkut Nov 08 '18 at 19:25
  • @Teemu @szkut also `if ((/\.(png|jpeg|jpg|gif)$/i).test(filteredAddedFiles[i].name))` could be evaluating to false – Katie.Sun Nov 08 '18 at 19:25
  • @szkut you have to check the length before you hit the for loop. on the line before – Katie.Sun Nov 08 '18 at 19:26
  • @Teemu @Katie.Sun - I commented out the `if` statment with `test` and add `console.log(filteredAddedFiles.length);` before `for` - and still it dosen't work (it working only while debug, but the console log don't show in console as well) – szkut Nov 08 '18 at 19:32
  • 1
    @szkut how are you calling this part of your code? if you just do a `console.log("here");` before the for loop and run it normally, what happens? Because if the answer is nothing then whatever is causing this problem is not in the block of code you posted – Katie.Sun Nov 08 '18 at 19:34
  • @Teemu I'm testing with chrome. @Katie.Sun - it's like you said - `console.log("here");` - and nothing -, I will write when I found something – szkut Nov 08 '18 at 19:41
  • 1
    Make sure the logs are shown, "Default" should be selected from "Log levels". – Teemu Nov 08 '18 at 19:53
  • I turn on logs in chrome console and all `console.log`'s appear - but there isn't any error – szkut Nov 08 '18 at 20:14
  • 1
    Where is `filteredAddedFiles` coming from? – Matti Price Nov 08 '18 at 21:53
  • @Matti Price, sedond edit post – szkut Nov 09 '18 at 11:25
  • The problem is, that the first filereader load listener is not executed when you return `out`. Hence the array is empty at the time you're trying to iterate it. Chrome console also works asynchronously, hover over the small blue i-icon to see more information. If the code works when the DevTools are actually opened, that maybe happens because logging takes a few microseconds which might be enough to populate `out`. See [How do I return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323). – Teemu Nov 09 '18 at 13:07

0 Answers0