0

I've been trying to code this in a way so that the values from the first query (referenced documentIDs) will be pushed into an array and then use a for loop to get the values from the array and use the documentIDs stored as part of the second query.

query.get().then(function(querySnapshot){
    querySnapshot.forEach(function(doc){
        reviewData.push(doc.data())
        
        })
        console.log(reviewData)
    })
    console.log(reviewData)  

However, once the array is accessed outside of the get() function it returns it values as undefined despite it showing that the array has values. This is the result of the console.log outside the get() function

enter image description here

and this is the result for the console.log inside the get() function enter image description here

One thing that i noticed though is that it seems the console.log from outside the get() function seems to be registered as an empty array despite having supposed values in it. How do i implement this properly so that I would be able to get the values of array.placesDocument and array.reviewDocument and use that as as a .doc() reference for a second and third query. Other solutions are also highly appreciated. Thanks!

Community
  • 1
  • 1
  • 6
    `get()`, and any function that returns a promise, will return immediately before the work is complete. You can only count on the results of the work being available inside your callback, after it finally finishes. You might want to spend some time learning about how JavaScript promises work, as this is very common. – Doug Stevenson Mar 13 '20 at 20:12
  • Yes. I actually tried to implement this using promises and made different activities using promises but i just can't get it to work with firestore queries specifically – Swagini Houdini Mar 13 '20 at 21:31
  • 1
    Any code that requires the data from Firestore needs to be inside the `then` callback, or be called from there. Since your `console.log(reviewData) ` is outside of that callback, it doesn't have access to the data from Firestore (or more correctly: when the `console.log` runs your `reviewData.push(doc.data())` hasn't run yet). Set some breakpoints, and you'll see that they trigger in a different order. Also see https://stackoverflow.com/a/57148528 – Frank van Puffelen Mar 13 '20 at 21:52
  • Thanks y'all I actually managed to properly implement the queries using promises but I'm experiencing 1 problem right now. I have an edit function that takes the parameter of the documentIDs (which i properly got at this point and is successfully pushed into an array) but whenever i call the edit function and pass those documentIDs as parameters it is considered as undefined and thus causes reference error. Do you know what I should do? – Swagini Houdini Mar 14 '20 at 06:35
  • 1
    @SwaginiHoudini Hello, for you last comment, please create a new question with a minimal, reproducible example, see https://stackoverflow.com/help/minimal-reproducible-example – Renaud Tarnec Mar 14 '20 at 07:59

0 Answers0