问题
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