-1

I have an array who's called files_url. I fill it like this:

var files_url = new Array(filesnb);
var files_name = new Array(filesnb);
for (var i = 0, k = 0; i < filesnb; i++) {
    files_name[i] = selectedFile[i].name;
    const uploadTask = storageRef.child(`${selectedFile[i].name}`).put(selectedFile[i]); 
    uploadTask.on('state_changed', (snapshot) => {
        var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
        console.log('Upload is ' + progress + '% done');
        }, (error) => {
            console.log(error);
        }, () => {
        uploadTask
            .then(snapshot => snapshot.ref.getDownloadURL())
            .then((url) => {
                //Here is the part where i fill my array "files_url"
                for (var len = 0; len < i; len++) {
                    if (check_right_url(files_name[len], url) === 0) {
                        files_url[len] = url;
                        console.log(i);
                        console.log(len);
                        break;
                    }
                }
            })
    });
}

When I console.log(files_url) I can see the right informations on my array, but when I try to do console.log(files_url[0]), it show me undefined. Why? How can I retrieve the content of the array?

Here is some screen:

When I do console.log(files_url):

enter image description here

enter image description here

When I do console.log(files_url[0]):

enter image description here

Zahreddine Laidi
  • 560
  • 1
  • 7
  • 20
  • Share your example array and `check_right_url` method – Void Spirit Oct 04 '18 at 15:29
  • 1
    What's `check_right_url`? Is it async? When are you logging? – Dave Newton Oct 04 '18 at 15:30
  • `check_right_url` is just a function that will check if my filename correspond to the correct url of the file, if my filename is at the third element of my array, so, it will put the url_file aswell at the third element of the `files_url` array. But my loop was created to not create an empty slot on my `files_url` array. And btw, `files_name` array and `files_url` array have the same slot array .@DaveNewton – Zahreddine Laidi Oct 04 '18 at 15:46
  • Your `for` loop is very weird. Usually `i` is the iteration variable and `len` is the limit. – Barmar Oct 04 '18 at 16:08
  • What's the value of `i`? I suspect it's `0`, so the loop is never doing anything, and you created the array with a single empty element. – Barmar Oct 04 '18 at 16:09
  • The result you're getting is what you'd see if you did `files_url = Array(1);`. – Barmar Oct 04 '18 at 16:10
  • The value of i correspond to the number of the file, so this is 1, but len is 0 so this is why i don't understant because it only write the url on the slot 0 of the array.. @Barmar – Zahreddine Laidi Oct 04 '18 at 16:12
  • Then I don't see how this result is possible, since the first iteration of the loop clearly fills in `files_url[0]`, so it can't be an empty slot. – Barmar Oct 04 '18 at 16:14
  • Please create a [mcve] that shows how you initialize all the variables. – Barmar Oct 04 '18 at 16:15
  • I edited my post, here is my entiere code now. @Barmar – Zahreddine Laidi Oct 04 '18 at 16:16
  • You say that you see the right information in the array when you do `console.log(files_url)`. But that shows `<1 empty slot>`. Is that really the expected information? – Barmar Oct 04 '18 at 16:20
  • Where is `console.log(files_url)` in the full code? I suspect you're doing it before the asynchronous operations have completed. – Barmar Oct 04 '18 at 16:21
  • She is after this code... @Barmar – Zahreddine Laidi Oct 04 '18 at 16:22
  • Put it inside the last `.then()` callback and you'll get the result you expect. – Barmar Oct 04 '18 at 16:23
  • I finally find where the problem was, i didn't finish to upload the file and i tell him to give me the download url file. MY BAD and thank you man @Barmar – Zahreddine Laidi Oct 04 '18 at 16:26

1 Answers1

2

If the "right URL" isn't the first one, you'll put it at an index in the array that isn't 0, so files_url[0] will be undefined (your screenshot shows it as an "empty slot;" when you read an empty slot, you get the value undefined). Like this:

var files_url = [];
files_url[1] = "file"; // Note 1, not 0
console.log(files_url[0]);

You probably wanted:

files_url.push(url);

or

files_url[files_url.length] = url;

instead.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I did this if i have multiple file_name to check, and see on the image, it show "length = 1", it means that the array doesn't have some slot who are not filled, so i don't understand why.. – Zahreddine Laidi Oct 04 '18 at 15:43
  • @ZahreddineLaidi - Sorry, I can't figure out what your `files_name` and `files_url` structures are if the above isn't the issue and none of this is asynchronous. Please update your question with a [mcve] demonstrating the problem, ideally a **runnable** one using Stack Snippets (the `[<>]` toolbar button; [here's how to do one](https://meta.stackoverflow.com/questions/358992/)). – T.J. Crowder Oct 04 '18 at 15:53
  • This is asynchronous. – Zahreddine Laidi Oct 04 '18 at 15:59
  • @ZahreddineLaidi - Then it's likely you're trying to do it before the data is available. We'll need to see that [mcve] to help you. Use `setTimeout` or similar to simulate asynchronousness. – T.J. Crowder Oct 04 '18 at 16:03
  • I already did a response to Dave Newton question, here it is : "check_right_url is just a function that will check if my filename correspond to the correct url of the file, if my filename is at the third element of my array, so, it will put the url_file aswell at the third element of the files_url array. But my loop was created to not create an empty slot on my files_url array. And btw, files_name array and files_url array have the same slot array ." – Zahreddine Laidi Oct 04 '18 at 16:04
  • @ZahreddineLaidi - Which reads as "no, this isn't async" (sorry, I managed to forget having seen that). But you've said it is, so... – T.J. Crowder Oct 04 '18 at 16:04
  • My code is really async, i know what i'm talking about, where do you see that my code isn't async ?? – Zahreddine Laidi Oct 04 '18 at 16:07