title: Firestore Compound Queries inside foreach statement
body: <p>I would like some help if possible, I want to do a Firestore compound query inside 2 foreach statements as I need to get all relevant data back that matches the contents in the arrays being looped through.</p>
  
  <pre><code>arenas.forEach((arenasElement: any) =&gt; {
      sports.forEach((sportsElement: any) =&gt; {
          firebaseAdmin.firestore().collection('leagues')
              .where('arenaUid', '==', arenasElement)
              .where('sportUid', '==', sportsElement)
              .get()
              .then((querySnapshot: any) =&gt; {
                  querySnapshot.docs.map((element: any) =&gt; {
                      leaguesArray.push(element.data())
                  })
              })
              .catch((error) =&gt; {
                  console.log(error)
                  res.send(error)
              })
      })
  })
  </code></pre>
  
  <p>I need all the relevant results to be stored in the array as all of them are objects &amp; I want to return that array when all of the queries are finished executing as I need all the data.</p>
  
  <p>I've tried looking at promises &amp; wrapping it but I had no success in the matter.</p>
  
  <p>Any help would be gladly appreciated!</p>
  
  <p>EDIT: The above got answered with below but I am struggling with another function I have</p>
  
  <pre><code>export const getSports = firebaseFunctions.https.onRequest((req: any, res: any) =&gt; {
  cors(req, res, () =&gt; {
      let sportsUidArray: any = []
      let sportsArray: any = []
      let arenas: any = []
      let promises: any = []
  
      arenas = req.body.arenas
  
      arenas.forEach((arenasElement: any) =&gt; {
          promises.push(
              firebaseAdmin.firestore()
                  .collection('leagues')
                  .where('arenaUid', '==', arenasElement)
                  .orderBy('title')
                  .get()
          )
      })
  
      Promise.all(
          promises
      ).then((querySnapshots: any) =&gt; {
          querySnapshots.forEach((snapshotElement: any) =&gt; {
              snapshotElement.docs.map((element: any) =&gt; {
                  sportsUidArray.push(element.data().sportUid)
              })
          })
  
          sportsUidArray = Array.from(new Set(sportsUidArray))
  
          promises = []
  
          sportsUidArray.forEach((sportIdElement: any) =&gt; {
              promises.push(
                  firebaseAdmin.firestore()
                      .collection('sports')
                      .doc(sportIdElement)
                      .get()
              )
          })
  
          Promise.all(
              promises
          ).then((querySnapshots2: any) =&gt; {
              querySnapshots2.forEach((snapshotElement: any) =&gt; {
                  /* Issue with line below */
                  snapshotElement.docs.map((sportsElement: any) =&gt; {
                      sportsArray.push({ id: sportsElement.id, data: sportsElement.data() })
                  })
              })
  
              res.send(sportsArray)
          }).catch((err: any) =&gt; {
              res.send(err)
              console.error(err)
          })
      }).catch((err: any) =&gt; {
          res.send(err)
          console.error(err)
      })
  })
  </code></pre>
  
  <p>})</p>
  
  <p>Error I'm receiving from Firebase:</p>
  
  <pre><code>TypeError: Cannot read property 'map' of undefined
  </code></pre>