0

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
  • Your data is only populated in the array when the `.then` is executed. If you need to "wait" for that, you need to wait for that with another `.then` (or use `async` and `await`). – crashmstr Oct 23 '19 at 17:54
  • That `__ob__` property of your `Array` is suspect; something may be augmenting your Array. Where does `this.id_firestore` come from? – Jacob Oct 23 '19 at 18:00
  • It's a variable in global context who a set **let id_firestore = []**. Maybe this variable is the trouble? @Jacob – lucas menezes Oct 23 '19 at 18:20
  • @crashmstr how can i did it? – lucas menezes Oct 23 '19 at 18:46
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – crashmstr Oct 24 '19 at 11:41

1 Answers1

0

Found the answer. I'm did this functions in a vue enviorment and because and under the table the variable runs observer standards. I Just have to make a internal var and run the other function inside it:

   function validation () {
      let id_firestore = []
      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)
        })
       this.search_doubts (id_firestore)
      }) 

    },

Thank's for the help!