I'm started to work with JavaScript/Firestore for a few days but i'm facing some issues with the behavior of the laguage in array's. I cannot run array's with a for loop inside a function and almost all the time it returns me an undefined. this is my code:
This is the code who takes all the id's on firestore who the meeting is active :
function validation () {
let path = firebase.firestore()
path
.collection('database')
.doc('405')
.collection('meetings')
.where('active', '==', true)
.get().then( snapshot =>{
snapshot.docs.forEach( snapshot => {
this.id_firestore.push(snapshot.id)
})
})
},
And this is the code who runs all the id's on id_firestore array and takes all the doubts of the meeting on firestore:
function search_doubts (){
let data = this.id_firestore // in this line data.value = ["QEq1VexdC28BbWRvSFL7","vFsSdDeHJqJQU13dwMwQ"]
let path = firebase.firestore().collection('database').doc('405').collection('meeting')
console.log(data) // here it logs me all the array normally
console.log(data[0]) // here the log returns me undefined
for (let i = 0; i<data.lenght; i++) {
path.doc(data[i]).collection('doubts').get().then( snapshot =>{
snapshot.docs.forEach( snapshot => {
this.doubts.push(snapshot.data().txt_doubts)
this.id_firestore_doubts.push(snapshot.id)
})
console.log(data[1]) //here it logs me the data normally
})
}
}
I did too many searchs but i didn't find anything about this behavior, can anybody answer why the function is behaving like this? This is the chrome console result's on my debbug:
Console.log(data):
[__ob__: Observer]
0: "QEq1VexdC28BbWRvSFL7"
1: "vFsSdDeHJqJQU13dwMwQ"
length: 2
__ob__: Observer {value: Array(2), dep: Dep, vmCount: 0}
__proto__: Array
Console.log(data[0]):
undefined
Console.log(data[1])
vFsSdDeHJqJQU13dwMwQ