Ad

Can't Access My Store From Within Firebase Promise

Within the promise attached to Firebase's createUserWithEmailAndPassword() function I'm trying to access a mutation function from my store, but I get the following error:

"TypeError: Cannot read property '$store' of undefined"

Why and how to solve this problem?

<script>
import firebase from 'firebase'

export default {
  name: 'Signup',
  data: function() {
    return {
      email: '',
      password: ''
    }
  },
  methods: {
    signUp: function() {
      firebase.auth().createUserWithEmailAndPassword(this.email, this.password).then(
        function(user) {
          alert('Your account has been created!');
          this.$store.userConnectedUpdate('true');
        },
        function(error) {
          alert('Oops. ' + error.message)
        }
      );
    }
  }
}
</script>
Ad

Answer

Inside the callback function this has a different meaning. There are a few broad solutions to this, but the simplest one is to use => notation:

signUp: function() {
  firebase.auth().createUserWithEmailAndPassword(this.email, this.password).then(() => {
      alert('Your account has been created!');
      this.$store.userConnectedUpdate('true');
    },
    function(error) {
      alert('Oops. ' + error.message)
    }
  );
}

See my answer here for the other options: firebase :: Cannot read property 'props' of null

Ad
source: stackoverflow.com
Ad