nuxt-community / firebase-module

🔥 Easily integrate Firebase into your Nuxt project. 🔥
https://firebase.nuxtjs.org
MIT License
641 stars 99 forks source link

Cannot read property '$fire' of undefined #569

Closed jareko999 closed 2 years ago

jareko999 commented 2 years ago

This isn't necessarily a bug, but I wanted to know, why am I getting Cannot read property '$fire' of undefined when called in my mutations.js file?

Is this because $fire is only accessible within .vue files? I can run this.$fire.firestore just fine in my pages, but not in my store files. Here is what I'm trying to do:

mutations.js

SET_FIRE_USER: (state, { authUser }) => {
     this.$fire.firestore.collection('users').get().then(snapshot => {
       var users = []
       snapshot.forEach(doc => {
         var docData = doc.data()
         users.push(docData)
       })
       state.users = users;
     })
 }

And I end up getting Cannot read property '$fire' of undefined. Do I need to import firebase in this case? How can I access $fire in my store files?

alexblunck commented 2 years ago

This should work if you don't use arrow functions. That way this is bound to the nuxt context:

SET_FIRE_USER(state, { authUser }) {
  this.$fire...
})
jareko999 commented 2 years ago

This works, thanks!