Ces*_*uez 4 javascript async-await firebase firebase-authentication
async login({ commit }, { email, password }) {
let result
try {
result = await firebase.auth().signInWithEmailAndPassword(email, password)
commit('setUser', result.user)
} catch (err) {
commit('setError', err)
}
}
Run Code Online (Sandbox Code Playgroud)
这是 Vuex 中的一个动作。当它运行时,我希望 是commit('seteError', err)从 返回的错误的有效错误处理程序catch (err)。相反,我得到一个“未捕获”异常并且执行停止。
希望得到任何人谁管理,使用任何的见解async与firebase.auth()
sma*_*oes 10
我今天遇到了同样的问题 - 这似乎是由于 firebase auth 使用的 Closure promise 库的实现。有一个在火力-JS-SDK项目的封闭问题在这里,上面有一些细节。最终,此植入所发生的事情是设置超时,引发超出范围的异常try/catch:
为了避免这种情况,需要通过调用.catch()返回的承诺来处理异常,例如:
firebase.auth().signInWithEmailAndPassword(email, password)
.catch((ex) => { /* handle exception */ });
Run Code Online (Sandbox Code Playgroud)
由于我已将所有 firebase 逻辑包装并集中,因此我最终做的是将 firebase Promise 包装在我自己的一个中:
export async function signIn(email, password) {
return new Promise((resolve, reject) => {
firebase.auth().signInWithEmailAndPassword(email, password)
.then((userCreds) => resolve(userCreds))
.catch((reason) => reject(reason));
});
}
Run Code Online (Sandbox Code Playgroud)
然后可以使用 async await 调用包装函数:
try {
const result = await signIn(email, password)
// Do what you need to with the result
} catch (ex) {
// Handle any exceptions
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1767 次 |
| 最近记录: |