Home > Software design >  Use Firebase uid in firestore (Vue)
Use Firebase uid in firestore (Vue)

Time:05-20

I'm using firestore, and I want to use a user's uid in the query, like this:

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

This doesn't work. I get this error:

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

I tried just passing the uid as a variable and fetching it in beforeMount(), but I get this error:

Cannot read properties of undefined (reading 'uid')

What should I do?

CodePudding user response:

The issue is with the vuefire binding:

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.

I had to use Programmatic binding, like this:

  const Cards = db.collection('Users')

...

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