首页 > 解决方案 > 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',
  },   

});

标签: react-nativeasyncstorage

解决方案


asyncStorage 中没有名为 getkeys 的函数

请参阅此链接以了解异步存储中的可用功能

getKeys = async () => {
    try{
      return await AsyncStorage.getKeys()
    }catch (e) {
      alert(e)
    }
  }

改成这个

getKeys = async () => {
    try{
      return await AsyncStorage.getAllKeys()
    }catch (e) {
      alert(e)
    }
  }

推荐阅读