firebase gotchas

some gotchas

collections -> documents -> collections

then those collections can't be listed down as querySnapshots, you have to explicitly call them by ID. if you have fields in the same branch, they are returned though.

for example:

// SETUP
import firebase from 'firebase'
const config = {
  //firebase config here
}
firebase.initializeApp(config)
// DB structure on firebase console
collection1
    - document1
        - collection1a
            - document1a1
        - collection1b
        - key1: value1
    - document2
        - collection2a
  1. when you query for all documents in collection1, you get only {"key1": "value1"} as your result, with no acknowledgement of the existence of document2
firebase
  .firestore()
  .collection('collection1')
  .get()
  .then(docs => {
    docs.forEach(doc => {
      console.log(doc.id)
      console.log(doc.data())
    })
  })
  1. to access collections that are nested under documents, you have to explicitly call them:
firebase
  .firestore()
  .collection('collection1')
  .doc('document1')
  .collection('collection1a')
  .get()
  .then(docs => {
    docs.forEach(doc => {
      console.log(doc.id)
      console.log(doc.data())
    })
  })

if you ever forget that sub-collection exists, there is no way to find out except by exploring the firestore through the firebase console yourself.

video describing orphaned sub-collections