在 firestore (Vue) 中使用 Firebase uid

Use Firebase uid in firestore (Vue)

我正在使用 firestore,我想在查询中使用用户的 uid,如下所示:

methods: {
  //...
},
firestore: {
  Cards: db.collection("Users").doc(firebase.getCurrentUser().uid).collection("Cards")
},
async beforeMount() {
  //...
}

这行不通。我收到此错误:

Uncaught (in promise) FirebaseError: Missing or insufficient permissions.

我尝试将 uid 作为变量传递并在 beforeMount() 中获取它,但出现此错误:

Cannot read properties of undefined (reading 'uid')

我该怎么办?

问题出在 vuefire 绑定上:

https://vuefire.vuejs.org/vuefire/binding-subscriptions.html#programmatic-binding

If you need to change the bound reference while the application is running, e.g. to display a different user profile, or different product detail page, Declarative binding isn't enough.

我不得不使用 程序化 绑定,如下所示:

  const Cards = db.collection('Users')

...

  watch: {
    uid: {
      // call it upon creation too
      immediate: true,
      handler(uid) {
        this.$bind('Cards', Cards.doc(uid).collection("Cards"))
      },
    },
  },