Search code examples
react-nativeasyncstorage

TypeError : undefined not an object - AsyncStorage


Hi I cant seem to solve this issue - I am using AsyncStorage in a React Native component to load file path strings. I am getting the below error when the component mounts and failing to see what I am doing incorrectly here as it pretty much follows the documented examples. any help would be appreciated.

"TypeError: undefined is not an object (evaluating '_asyncStorage.AsyncStorage.getKeys')

here is my code :

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',
  },   

});

Solution

  • There is no function called getkeys in asyncStorage

    refer this link to know the available functions in async storage

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

    change to this

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