0

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.

8
  • is the data that you are passing to the asyncstorage valid? are you getting any error Commented Dec 7, 2017 at 8:23
  • @PanagiotisVrs I think its valid. Not getting any error. Commented Dec 7, 2017 at 8:27
  • can you try callbacks like AsyncStorage.getItem('iext_inf', (err, result) => { }) Commented Dec 7, 2017 at 8:34
  • @PanagiotisVrs Tried it already. Not getting any error or result! Commented Dec 7, 2017 at 8:37
  • can i suggest try a static object like the example of react native ? let UID123_object = { name: 'Chris', age: 30, traits: {hair: 'brown', eyes: 'brown'}, }; Commented Dec 7, 2017 at 8:40

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

this is not valid. JSON.stringify turns the object to a string. There is no reason why this is not working for the AsyncStorage
The value that I was passing was null which caused AsyncStorage to stuck without throwing any errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.