2

I want to get each file's name from firebase storage and put in map array, so I got this code.

listRef.listAll().then((res) => {
    res.items.forEach((itemRef) => { 
      //console.log(itemRef);
      
      itemRef.getMetadata().then((metadata) => {

        itemName = metadata.name;
        map.set(i,itemName);
        i++;
      
      }).catch((error) => {
        console.log(error);
      });

    });

  }).catch((error) => {
    console.log(error);
  });

  console.log(map); //it shows Map(0) but have entries in it
  map.forEach((value,key) => {
    console.log("123") //nothing happened
});

I successfully got names all in the map, but it shows Map(0) and have those names in its entries... so I can't get anything in forEach cycle, do anyone know why does it occur and how to solve it?

kittenBark
  • 31
  • 5
  • 1
    to answer the `it shows Map(0) but have entries in it` ... when you log an Object to the console and inspect it, the console shows the current state of the Object, not the state when it was logged - due to the asynchronous nature of the data being added to the Object, this can get confusing – Bravo Aug 13 '21 at 03:16
  • I am experiencing exactly the same thing. I can use console.log to confirm there are 6 entries in my map and it even shows a size of 6, but if I then try mapName.size as a follow-up log entry I get zero. I even did a follow up log entry of the entire map again after that and again get size of 6 and see the entry details when i expand. as a result I too cannot use for/of to loop over my map's entries. did you figure this out? – user3064141 Jul 01 '22 at 22:35

0 Answers0