react-native - TypeError:未定义不是对象 - AsyncStorage
问题描述
嗨,我似乎无法解决这个问题 - 我在 React Native 组件中使用 AsyncStorage 来加载文件路径字符串。当组件安装时我收到以下错误并且看不到我在这里做错了什么,因为它几乎遵循了记录的示例。任何帮助,将不胜感激。
“TypeError:未定义不是对象(评估'_asyncStorage.AsyncStorage.getKeys')
这是我的代码:
import React, { Component } from 'react';
import BasicCard from './cards'
import {AsyncStorage} from '@react-native-community/async-storage'
import {
StyleSheet,
ScrollView,
} from 'react-native';
class NewPost extends Component{
state={
content:[]
}
componentDidMount(){
this.getContent(this.getKeys())
}
getKeys = async () => {
try{
return await AsyncStorage.getKeys()
}catch (e) {
alert(e)
}
}
getContent = async (keys) => {
try{
await AsyncStorage.multiGet(keys, store)
this.setState({content:store})
} catch (e) {
alert ( e )
}
}
render(){
return(
<ScrollView style={styles.container}>
{this.state.content.map((result,i,item) => (
<BasicCard
imgName={item[i][0]}
imgPath={item[i[1]]} />
))}
</ScrollView>
)
}
}
export default NewPost;
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
backgroundColor: 'black',
},
});
解决方案
asyncStorage 中没有名为 getkeys 的函数
getKeys = async () => {
try{
return await AsyncStorage.getKeys()
}catch (e) {
alert(e)
}
}
改成这个
getKeys = async () => {
try{
return await AsyncStorage.getAllKeys()
}catch (e) {
alert(e)
}
}
推荐阅读
- regex - 正则表达式以防止某些电话区号
- android - 连接 WiFi 时如何使用移动数据服务?
- python-3.x - 将通过html表单发送的xlsx转换为python中的csv/tsv
- outlook - 使用 MAPI 发送到 Outlook 2013 有什么特别之处吗?
- python - 将 Django 表单数据保存到用户
- r - 当 barplot 正确绘制所有内容时,ggplot 将所有数据绘制为单列
- c++ - scoped_lock 可以在读取模式下锁定 shared_mutex 吗?
- ruby-on-rails - Rails 5 将环境变量读取为 YAML 文件中的字符串
- vb.net - 如何在打开的窗口外阻止鼠标点击?
- tensorflow - 张量流概率。- 保存和恢复贝叶斯神经网络的检查点