firebasegoogle-cloud-firestorevuexfire

How do I get firestore collection reference from within then() function?


I am using vuexfire to bind firebase references to my app state.

This works fine:

bindRef: firebaseAction(({bindFirebaseRef}, payload) => {
let firebaseRef = db.collection(`/${payload}`)
  bindFirebaseRef('storeProperty',firebaseRef)
})

I, however, only want to do the binding after a successful get; just so that I can be able to catch errors and also set progress indication.

Something like this:

bindRef: firebaseAction(({bindFirebaseRef}, payload) => {
  let firebaseRef = db.collection(`/${payload}`).get().then(e => {
   //where ref is same as firebaseRef
    bindFirebaseRef('questions',ref) 
  })
})

Solution

  • You need to declare the reference to that collection as a variable and only then you can pass it on to your function:

    bindRef: firebaseAction(({bindFirebaseRef}, payload) => {
      let firebaseRef = db.collection(`/${payload}`)
      firebaseRef.get().then(e => {
       //pass firebaseRef to the function
        bindFirebaseRef('questions',firebaseRef) 
      })
    })