Firebase REST API auth不能与React一起工作?

问题描述 投票:0回答:1

基本上,我和一个朋友一直在研究我们的React项目,我们在后端使用Redux和Redux Thunk来处理验证。然而,我们似乎遇到了一个问题。我们的请求之前是正常的,但现在它发出了一个Fetch Failed Loading: POST, 并没有继续经过初始调用. 然而,当检查Firebase时,它返回了正确创建的用户。我知道它没有通过获取,因为 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个回答
0
投票

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

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

方便抓取错误


0
投票

如果在获取中使用了async。需要这样做

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

在获得响应对象后,需要通过等待来获取响应中的正文内容。以下是描述


0
投票

因为你使用的是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.orgen-USdocsWebAPIFetch_API。

https:/developer.mozilla.orgen-USdocsWebJavaScriptReferenceGlobal_ObjectsPromise。

© www.soinside.com 2019 - 2024. All rights reserved.