【问题标题】:AsyncStorage.getItem return undefined when Debug JS Remotely is turned off -- React Native当 Debug JS Remotely 关闭时,AsyncStorage.getItem 返回 undefined -- React Native
【发布时间】:2018-04-17 18:26:36
【问题描述】:

我在使用 AsycnStorage 时遇到了一个非常奇怪的问题。我正在尝试使用保存在 AsycnStorage 上的身份验证令牌发送 4 个请求,问题是我只获得了一次令牌,在第一个请求中工作正常,但在第二个请求中,AsycnStorage 返回未定义的值。这很奇怪,因为只有在远程调试 JS 关闭时才会发生,如果我使用远程调试 JS 进行测试,它就可以正常工作。它也只发生在 iOS 上,在 Android 上可以正常使用 Debug JS Remotely 打开和关闭。

export async function fetchApi(endPoint, payload = {}, method = 'get', headers = {}){
const accessToken = await AsyncStorage.getItem('token');
console.log(accessToken); 
return fetchival(`${apiConfig.url}${endPoint}`, {
    headers: _.pickBy({
        ...(accessToken ? {
            Authorization: `Bearer ${accessToken}`,
        } : {}),
        ...headers,
    }, item => !_.isEmpty(item)),
})[method.toLowerCase()](payload)
.catch((e) => {
    if (e.response && e.response.json) {
        e.response.json().then((json) => {
            if (json) throw json;
            throw e;
        });
    } else {
        throw e;
    }
});

};

此代码适用于:
远程打开 Debug JS 的 Android
带有 Debug JS 的 Android 远程关闭
远程打开 Debug JS 的 iOS

【问题讨论】:

  • 您遇到的错误是什么?

标签: react-native react-native-ios


【解决方案1】:

我修复了它,我的问题是一些请求没有在“/”上完成,例如我点击了api/{id_user}/postsapi/{id_user}/posts/ 的实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-28
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多