AsyncStorage.getItem return 当远程调试 JS 关闭时未定义 -- React Native
AsyncStorage.getItem return undefined when Debug JS Remotely is turned off -- React Native
我在使用 AsycnStorage 时遇到了一个很奇怪的问题。我正在尝试使用保存在 AsycnStorage 上的身份验证令牌发送 4 个请求,问题是我只获得了一次令牌,在第一个请求中工作正常但在第二个请求中 AsycnStorage return 未定义为值.这很奇怪,因为只有在关闭 Debug JS Remotely 时才会发生这种情况,如果我在打开 Debug JS Remotely 的情况下对其进行测试,则效果很好。它也发生在 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;
}
});
};
此代码适用于:
Android 远程调试 JS 打开
Android 远程调试 JS 关闭
iOS 远程调试 JS 开启
我修复了它,我的问题是一些请求没有在“/”上完成,例如我正在访问 api/{id_user}/posts/
的 api/{id_user}/posts
个实例。
我在使用 AsycnStorage 时遇到了一个很奇怪的问题。我正在尝试使用保存在 AsycnStorage 上的身份验证令牌发送 4 个请求,问题是我只获得了一次令牌,在第一个请求中工作正常但在第二个请求中 AsycnStorage return 未定义为值.这很奇怪,因为只有在关闭 Debug JS Remotely 时才会发生这种情况,如果我在打开 Debug JS Remotely 的情况下对其进行测试,则效果很好。它也发生在 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;
}
});
};
此代码适用于:
Android 远程调试 JS 打开
Android 远程调试 JS 关闭
iOS 远程调试 JS 开启
我修复了它,我的问题是一些请求没有在“/”上完成,例如我正在访问 api/{id_user}/posts/
的 api/{id_user}/posts
个实例。