React Native AsyncStorage not getting data

匆匆过客 提交于 2020-02-08 07:40:13

问题


I am using following code for storing and retrieving data. But it isn't working as I want.

Setting data:

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");
}
}

Getting data:

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.


回答1:


AsyncStorage only takes string as a value and key.

You are passing an object try to set string not an object. I am not sure JSON.stringfy() works for AsyncStorage.



来源:https://stackoverflow.com/questions/47690402/react-native-asyncstorage-not-getting-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!