我在我的操作文件中使用AsyncStorage设置了一个'session‘变量:
axios
...
// Session variable
AsyncStorage.setItem('userID', response.data.toString()).then((user) => {
this.setState({ user });
});
// Send to next page
NavigationService.navigate('Main');
然后,在我的页面中,我尝试获取值:
...
render() {
AsyncStorage.getItem('userID')
.then((value) => {
const data = JSON.parse(value);
console.log('userID ', data.name);
});
...
它返回'userID undefined‘。为什么会发生这种情况?
谢谢
发布于 2019-06-27 03:15:17
您所编写的代码的问题在于,userId
的值为10
,但是当您调用console.log
时,您将其视为具有name
属性的对象。正确的代码应该如下所示:
...
render() {
AsyncStorage.getItem('userID')
.then((value) => {
const userId = JSON.parse(value);
console.log('userID ', userId); // This line changed
});
...
https://stackoverflow.com/questions/56777686
复制相似问题