AsyncStorage无法解析或拒绝

问题描述 投票:3回答:4

我正在尝试从异步存储中获取项目。

componentDidMount() {
    console.log('componentDidMount')
    AsyncStorage.getItem(STORAGE_KEY)
      .then(storage => console.log('then', storage))
      .catch(err => console.log('catch', err))
}

也尝试这种方式:

componentDidMount() {
    console.log('componentDidMount')
    AsyncStorage.getItem(STORAGE_KEY, (err, data) => {
      console.log('data', data);
      console.log('err', err)
    });
 }

但无论我怎么做,调试器只输出componentDidMount日志,我根本没有得到结果。

知道这里有什么问题吗?

编辑:我通过世博会运行应用程序

react-native asyncstorage
4个回答
1
投票

世博会出现问题。完全重启expo后,我最初发布的相同代码工作正常。


0
投票

你应该试试这个:

async componentDidMount(){  
  await AsyncStorage.getItem(STORAGE_KEY)  
    .then( (value) =>{  
       console.warn(value);  
    }  
 );  
}

希望它有效。


0
投票

AsyncStorage是一个异步函数。它意味着您想在函数中使用它,它必须是异步的。使其异步的方法是在函数名称之前放置async关键字。

async componentDidMount() {
    console.log('componentDidMount')
    AsyncStorage.getItem(STORAGE_KEY)
      .then(storage => console.log('then', storage))
      .catch(err => console.log('catch', err))
}

这应该是这样的。问候


0
投票

我有android的问题,我只能解决它捆绑。

react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

我在package.json的脚本中添加了这个命令。

© www.soinside.com 2019 - 2024. All rights reserved.