Firebase REST API身份验证无法与React一起使用?

时间:2020-06-03 01:51:45

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

基本上是一个朋友,我一直在从事我们的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);

3 个答案:

答案 0 :(得分:0)

如果使用了异步, 建议使用try {做某事...} catch(e){}格式进行编写。

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

易于发现错误

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

由于您使用的是提取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