0

Hi I can't get the value of a specific index of an array. I don't know why because if I try to log the entire array it works but with a specific index it doesn't work

This is my code, I use cloud firestore to get the ID of a document and save it into array

var idUsers = [];
const users_list = document.querySelector("#users_list");

db.collection("utenti").get().then(function(querySnapshot) {

    querySnapshot.forEach(function(doc) {
        users_list.innerHTML += "<a href='utente.html' class='collection-item black-text'>" + doc.data().nome + " " + doc.data().cognome + " " + " </a>";
        idUsers.push(doc.id);
      });
});

console.log(idUsers);
console.log(idUsers[0]);

This is the result in Chrome console enter image description here

Raul
  • 135
  • 1
  • 12
  • see here https://stackoverflow.com/questions/42991854/console-logarray-returns-filled-array-but-console-logarray-length-is-0 – Maher Fattouh Jul 06 '19 at 10:50

1 Answers1

-1

Although it is considered to be a confusing statement, I assume that the operation is running asynchronously. Try to put a delay and then find value of idUsers[0] in order to find out the assumption.

Mahyar Mottaghi Zadeh
  • 1,178
  • 6
  • 18
  • 31