本文介绍了React Native AsyncStorage 未获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码来存储和检索数据.但它不能如我所愿.
I am using following code for storing and retrieving data. But it isn't working as I want.
设置数据:
async setDataFromApi(token, json) {
const {goBack} = this.props.navigation; // Navigation
let iext_inf = {
token: token,
userid: json.data.id,
username: json.data.username,
image: json.data.profile_picture,
};
console.log(iext_inf);
try {
await AsyncStorage.setItem('iext_inf', JSON.stringify(iext_inf) ); // Stuck here
goBack();
} catch (error) {
console.log("Error");
}
}
获取数据:
async componentWillMount() {
try {
const value = await AsyncStorage.getItem("iext_inf"); //Stuck here
console.log(value);
if (value !== null){
this.setState({
data : value
})
}
if ( this.state.data !== null ) {
this.props.navigation.navigate('Home', {'access_token': value});
}
} catch (error) {
console.log(error);
}
}
我做错了什么?我尝试添加单个数据并检索它并且由于某种原因它起作用了.现在不是了.提前致谢.
What am I doing wrong? I tried adding single data and retrieving it and it worked for some reason. Now it's not.Thank you in advance.
推荐答案
您正在传递一个对象,尝试设置字符串而不是一个对象.我不确定 JSON.stringfy()
是否适用于 AsyncStorage
.
You are passing an object try to set string not an object. I am not sure JSON.stringfy()
works for AsyncStorage
.
这篇关于React Native AsyncStorage 未获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!