Given a collection called 'Colors' on firestore, I want to get the doc IDs of each document (entry) in the collection and add that to an array.
I've tried many things, but this what my current attempt.
getVocab(){
var vocab = [];
this.db.collection("Colors").get().then((snapshot) => {
snapshot.docs.forEach((doc) => {
var str = "";
var currentID = str.concat(doc.id);
vocab.push(currentID);
});
for (let i = 0; i < 12; i++) {
console.log (vocab[i]);
}
return vocab;
});
}
ngOnInit() {
var vocab = this.getVocab();
for (let i = 0; i < 12; i++) {
console.log (vocab[0]);
}
}
Whenever I console.log within the getVocab() function anywhere before that final });
, it works i.e the array vocab
contains the document-id of all the documents in my Colors collection.
However, I get the following error whenever I try to use the returned array in any other method at all.
ERROR Error: Uncaught (in promise): TypeError: Cannot read property '0' of undefined
TypeError: Cannot read property '0' of undefined
Apparently, the array vocab is undefined when I return it, even though it isn't undefined right before I return it.
I'm sure this is some firebase rule that I don't understand :(