gdg-tangier / vue-firestore

:cloud: Cloud Firestore binding in realtime with Vuejs
MIT License
235 stars 28 forks source link

How to trigger an event after db.collection('mycoll') is called ? #23

Closed ramstein74 closed 5 years ago

ramstein74 commented 6 years ago

How to trigger an event after db.collection('mycoll') is called ?

I´m using a vuetify data-table with the loading indicator and need to set it to false after the table is populated.

regards

amranidev commented 6 years ago

Hi @ramstein74 Thank for opening this issue.

Basically, you can bind your collection manually by calling $binding which returns a promise, once the promise is resolved, you can write the logic you want.

....
mounted () {
    var source = firebase.firestore().collection("MyCollection")
    this.$binding('MyCollection', source).then((collection) => {
       // write the logic
    })
}
....

Thank you!

pongsakorn-onsri commented 6 years ago

Hi @amranidev

I found when the collections not have anything (I use where to filter it) then not resolve to execute the logic in closure

amranidev commented 6 years ago

Hi @Refereer, can you show me your code?

pongsakorn-onsri commented 6 years ago
   mounted () {
    // Binding Collections
    this.$binding('payments', this.paymentFilteredDatabase)
      .then((payments) => {
        this.isLoaded = true
        this.$emit('isLoaded', true)
        if (payments.length > 0) {
          console.log('payments: ', payments)
        } else {
          console.log('there no payments on this accounts email: ', this.user.email)
        }
      })
  },
  computed: {
    paymentFilteredDatabase () {
      if (this.isAdmin) {
        return db.collection('payments')
      }
      return db.collection('payments').where('user_email', '==', this.user.email)
    }
  }
amranidev commented 5 years ago

if the promise is not resolved, I believe there must be an error or an exception, can you add .catch to the promise and see what kind of error it throws.

Thanks!