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
Related Questions
- → How can I query Firebase for an equalTo boolean parameter?
- → How can I access nested data in Firebase with React?
- → Firebase simple blog (confused with security rules)
- → Removing item in Firebase with React, re-render returns item undefined
- → AngularJS Unknown Provider Error (Firebase & AngularFire)
- → How do you pass top level component state down to Routes using react-router?
- → "this" is null in firebase query function in reactjs
- → Angular Module Failed to Load
- → Multiple dex files define Lcom/google/android/gms/internal/zzrx;
- → Joining Firebase tables in React
- → How can I make add firepad to my reactjs project?
- → How to use Cloud Functions for Firebase to prerender pages for SEO?
- → React.js component has null state?
Ad