admin管理员组文章数量:1327665
async login({ mit }, { email, password }) {
let result
try {
result = await firebase.auth().signInWithEmailAndPassword(email, password)
mit('setUser', result.user)
} catch (err) {
mit('setError', err)
}
}
This is an action in Vuex. When this runs, I expect the mit('seteError', err)
to be a valid error handler for the error returned from catch (err)
. Instead, I get an 'Uncaught' exception and execution stops.
Would appreciate any insights from anyone who managed to use async
with firebase.auth()
async login({ mit }, { email, password }) {
let result
try {
result = await firebase.auth().signInWithEmailAndPassword(email, password)
mit('setUser', result.user)
} catch (err) {
mit('setError', err)
}
}
This is an action in Vuex. When this runs, I expect the mit('seteError', err)
to be a valid error handler for the error returned from catch (err)
. Instead, I get an 'Uncaught' exception and execution stops.
Would appreciate any insights from anyone who managed to use async
with firebase.auth()
-
Please include your Exception/Error as a Code Sample (
{}
) in your Question. Is the Exception really just'Uncaught'
? – Luca Kiebel Commented Aug 13, 2018 at 9:38 -
Indeed, that's the whole message: Uncaught K {ob: Observer}. Here K is the error object returned by
err
, which I'm passing tomit
– Cesar Martinez Dominguez Commented Aug 13, 2018 at 9:43 -
1
I have the same issue.
Uncaught L {code: "auth/invalid-email", message: "The email address is badly formatted."}
. Cesar, did you ever find an answer? – Justin Noel Commented Oct 23, 2018 at 11:58 -
I'm having the same issue.
Uncaught Bg {code: "auth/account-exists-with-different-credential", message: "An account already exists with the same email addr…ng a provider associated with this email address.", email: "XXXXXX", credential: Vf}
. I am catching the error and can log it, but it still shows up as uncaught... – Pascal Commented Jan 8, 2019 at 22:50
2 Answers
Reset to default 11I ran into the same issue today - it appears to be due to the implementation of the Closure promise library that firebase auth uses. There is a closed issue in the firebase-js-sdk project here with some details on it. Ultimately, what is happening with this implantation is a timeout is getting set that throws an exception outside of the scope of the try/catch
:
To avoid this, the exception needs to be handled by calling .catch()
on the returned promise e.g.:
firebase.auth().signInWithEmailAndPassword(email, password)
.catch((ex) => { /* handle exception */ });
As I have all of my firebase logic wrapped and centralized, what I ended up doing is wrapping the firebase Promise in one of my own:
export async function signIn(email, password) {
return new Promise((resolve, reject) => {
firebase.auth().signInWithEmailAndPassword(email, password)
.then((userCreds) => resolve(userCreds))
.catch((reason) => reject(reason));
});
}
The wrapper function can then be called using async await:
try {
const result = await signIn(email, password)
// Do what you need to with the result
} catch (ex) {
// Handle any exceptions
}
This is how I managed to handle firebase authentication with async in Vuex. I needed to use onAuthStateChanged
async login({ mit, dispatch }, { email, password }) {
try {
await firebase.auth().signInWithEmailAndPassword(email, password)
await dispatch('session')
} catch (err) {
mit('setError', err)
}
}
Where the 'session' dispatch simply does:
async session({ mit }) {
let user
try {
user = await userInSession()
mit('setUser', user)
} catch (err) {
console.error(err)
mit('setUser', {})
}
}
and the userInSession function uses onAuthStateChanged
:
const userInSession = function () {
return new Promise((resolve, reject) => {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
resolve(user.uid)
} else {
reject(Error('No user authenticated'))
}
})
})
}
本文标签: javascriptGetting 39Uncaught39 after catching error in firebaseauth() with asyncStack Overflow
版权声明:本文标题:javascript - Getting 'Uncaught' after catching error in firebase.auth() with async - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742214482a2434315.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论