React Native fetch 在另一个 fetch 回调中不起作用

React Native fetch doesn't work in another fetch callback

如果我从第 1 点调用我的 api 函数,api 方法中的 fetch 方法运行良好。当我将其注释掉并在 POINT 2 处调用函数时,在 addAccount() 中获取方法 不起作用。在 Reactotron 上没有例外,没有拒绝,没有请求,甚至我在 Charles Proxy 上也找不到请求。有什么区别,我必须知道什么才能弄明白?

我尝试使用 RN 0.55.2 和 0.57.5

// Auth.js typical react native component

import * as api from '../actions/api';

class Auth extends Component {

    // first triggered function
    loginAccount(){
        // api.addAccount();  // POINT 1 - this line works well if I uncomment
        fetch('https://domain-a.com/login/',{
                method: 'POST',
                credentials: "same-origin",
                headers: {
                    'accept-language': 'en-US;q=1',
                    'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
                },
                body: encodeURIComponent(bodyParameters)
            }).then((response) => {
                console.log(response);
                return response.json()
            }).then(({ status, invalid_credentials }) => {
                if(status == "ok"){
                    CookieManager.get('https://domain-a.com')
                        .then((cookies) => {
                            this.fetchAccountData(cookies);
                        })
            })

    }

    fetchAccountData(cookies){
        fetch('https://domain-a.com/'+cookies.user_id+'/info/',{
            method: 'GET',
            headers: {
                'cookie': cookies
                }
        }).then((response) => {
            return response.json();
        })
        .then(({ user, status }) => {
            api.addAccount(); // POINT 2 - this line doesn't work
        });
    }
}


// api.js 
// I repleaced fetch code with document example just to be clearify

export const addAccount = () => {
    console.log("fetch begin"); // always works
    fetch('https://facebook.github.io/react-native/movies.json')
    .then((response) => response.json())
    .then((responseJson) => {
        console.log(responseJson);  // won't works from point 2
    })
    .catch((error) =>{
        console.error(error); // never runs
    });
}

您在 addAccount() 函数中的第一个 .then 语句似乎缺少 return 语句。如果没有适当的 a 'return response.json()' 语句,responseJson 将是未定义的。还添加括号以获得更好的语义格式。

export const addAccount = () => {
console.log("fetch begin"); // always works
fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => {
    console.log(response); //test this response
    return response.json();
})
.then((responseJson) => {
    console.log(responseJson);  // won't works from point 2
})
.catch((error) =>{
    console.error(error); // never runs
});

}