0

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>
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
drake035
  • 3,955
  • 41
  • 119
  • 229

1 Answers1

1

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807