Shihab
Shihab

Reputation: 2679

React Native AsyncStorage not getting data

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.

Upvotes: 0

Views: 762

Answers (1)

Kaan Öner
Kaan Öner

Reputation: 82

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.

Upvotes: 1

Related Questions