Firebase API 在 react native 上使用 Axios 无法响应常见错误代码



我正在使用Axios和Redux使用Firebase Auth REST API和React Native。

我有一个用电子邮件和密码登录的操作,这是我的代码:

export function signIn(data){
const request = axios({
method:'POST',
url:SIGNIN, 
data:{
email: data.email,
password: data.password,
returnSecureToken:true 
},
headers:{
"Content-Type":"application/json"
}
}).then(response => {
return response.data
}).catch( e => {
console.log(e)
return false
});
return {
type: SIGN_IN_USER,
payload: request
}
}

如果我正确插入带有电子邮件和密码的凭据,一切都很好。

但是,如果我插入一封有错误的电子邮件,我希望得到文档中所示的答案,例如:

EMAIL_NOT_FOUND:没有与此标识符对应的用户记录。用户可能已被删除。

INVALID_PASSWORD:密码无效或用户没有密码。

USER_DISABLED:管理员已禁用用户帐户。

但只有当我生成一些错误时,我才会收到这个答案:

错误:请求失败,状态代码为400

与文档中给出的答案完全不同。

有人能帮我吗?谢谢

解决方案:

console.log(error.response.data.error.message(

我终于有了解决方案,只需检查您是如何导入firebaseConfig.js的,在那里您已经初始化了您的firebase。

大多数情况下,我们导出默认的firebaseConfig并将其作为对象导入:

import { firebaseConfig } from './src/config/FirebaseConfig'; // Incorrect
import FirebaseConfig from './src/config/FirebaseConfig'; // Correct Order

最新更新