【问题标题】:Firebase REST API auth not working with React?Firebase REST API 身份验证不适用于 React?
【发布时间】:2020-06-03 01:51:45
【问题描述】:

基本上,我和一个朋友一直在处理我们的 React 项目,我们在后端使用 Redux 和 Redux Thunk 来处理身份验证。然而,我们似乎遇到了一个问题。我们的请求以前有效,但现在它发出 Fetch Failed Loading: POST,并且在初始调用之后不会继续。但是,在检查 Firebase 时,它​​会返回正确创建的用户。我知道它不会超过 fetch,因为 console.log 根本不起作用。

export const signup = (email, password) => {


return async dispatch => {
    const response = await fetch('https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=[API-KEY]',
    {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            email: email,
            password: password,
            returnSecureToken: true 
        })
    }
    );


    console.log(response);

【问题讨论】:

  • 你能给我们完整的错误信息吗?

标签: reactjs firebase redux firebase-authentication redux-thunk


【解决方案1】:

如果使用异步, 建议使用 try{do await something...}catch(e){} 格式编写。

try{
    do await something
 }catch(e){
    error something
}

容易发现错误

【讨论】:

  • 它没有捕获任何东西:/ 我执行了一个 catch (e) 并控制台记录了 e,但没有得到任何回报。
【解决方案2】:

如果在 fetch 中使用了异步。需要这样做

let response = await fetch(You_URL)
let json = await response.json()

得到响应对象后, 它需要等待才能从响应中获取正文内容 下面的链接有说明 地址:Here is the description

【讨论】:

  • 不幸的是,这也不起作用:(。就像我之前提到的,这段代码的 sn-p 在前 10 次身份验证中运行良好,但现在已经完全停止运行
  • 我暂时没有遇到这种情况,不好意思,帮不了你
【解决方案3】:

由于您使用的是 fetch API,您需要再次执行await

export const signup = (email, password) => {


return async dispatch => {
    const response = await fetch('https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=[API-KEY]',
    {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            email: email,
            password: password,
            returnSecureToken: true 
        })
    }
    );

const data = await response.json();

console.log(data );

参考资料:

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

【讨论】:

    猜你喜欢
    • 2021-08-23
    • 1970-01-01
    • 2022-01-05
    • 2019-08-17
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    相关资源
    最近更新 更多