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

552 Views Asked by At

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) 
  })
})
1

There are 1 best solutions below

0
On BEST ANSWER

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) 
  })
})