0π
As explained in the documentation, signUserUp
should return a promise, in order for to chain it with this.$store.dispatch('signUserUp', userProfile).then(...)
. Firebase supports promises where possible. Itβs possible to chain once()
and flatten promise chain with async..await
:
async function signUserUp({commit}, payload) {
try {
...
const snapshot = await firebase.database().ref('users/' + slug).once('value');
...
const cred = await firebase.auth().createUserWithEmailAndPassword(payload.email, payload.password);
...
} catch (error) {
commit('setLoading', false)
commit('setError', error)
}
}
0π
If you need to wait on the asynchronous code in your action to complete then your action should return a Promise
.
Probably the simplest way* to make this work with your current code is to wrap it in a new Promise
.
signUserUp({commit}, payload) {
return new Promise(function(resolve, reject) {
/* existing function body */
});
}
Then add calls to resolve()
when the asynchronous code has completed. (For example, at this point: console.log('user profile uploaded')
or after one of the errors.) Once you call the resolve callback it will invoke the then
handler in your signup method. You can reject the Promise
as well using the reject callback. In your case however it looks like you want the action to handle the error itself and not propagate it.
*Since it looks like the firebase API uses promises you could also attempt to return the result of the firebase functions.