如何从 then() 函数中获取 firestore 集合引用?

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

我正在使用 vuexfire 将 firebase 引用绑定到我的应用程序状态。

这很好用:

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

但是,我只想在成功获取后进行绑定;这样我就可以捕获错误并设置进度指示。

像这样:

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

您需要将对该集合的引用声明为变量,然后才能将其传递给您的函数:

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