1

I am attempting to load and retrieve local data through asynchronous storage. I've laid out the current set up on a page, but am unable to retrieve data. Data is definitely saved as I receive a notification about it once I click store data. But when I proceed to call data it returns a null value. What am I doing something wrong here?

const STORAGE_KEY = '@save_enableauto'

    state={
        enableAuto:false
    }

    _storeData = async enableAuto => {
        try {
          let x = toString(enableAuto);
          await AsyncStorage.setItem(STORAGE_KEY, x)
          alert('Data successfully saved!')
          //this.setState({ enableAuto: x })
        } catch (e) {
            console.log(e)
          alert('Failed to save name.')
        }
      }

    _retrieveData = async () => {
        console.log('trying to call data')
        try {
          const enableAuto = await AsyncStorage.getItem('STORAGE_KEY');
          console.log(enableAuto)
          if (enableAuto !== null) {
            // We have data!!
            console.log(enableAuto);
          }
        } catch (error) {
            alert('failed to load previous settings.')
          // Error retrieving data
        }
    };

   ...

   <Button title={'Store Data'} onPress={() => this._storeData(this.state.enableAuto)}/>
                        <Button title={'call Data'} onPress={() => this._retrieveData()}/>
                    </View>    
                        <View>
                            <CheckBox
                                value={this.state.enableAuto}
                                onValueChange={() => this.setState({ checked: !this.state.enableAuto })}
                            />
2
  • what is the type of data that you are storing? is it other than string? Commented Dec 28, 2019 at 6:09
  • as shown in the state, it's a boolean which I turn into a string to be able to store Commented Dec 28, 2019 at 6:15

1 Answer 1

1

The error above is while you are storing, you are setting :

await AsyncStorage.setItem(STORAGE_KEY, x)

and here STORAGE_KEY refers to value @save_enableauto

But while you are retrieving data

const enableAuto = await AsyncStorage.getItem('STORAGE_KEY');

you are giving 'STORAGE_KEY' as string , so that means its refering 'STORAGE_KEY' and it doesnt have any stored value. Try this

const enableAuto = await AsyncStorage.getItem(STORAGE_KEY);

Hope it helps. feel free for doubts

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah this makes sense. I implemented your suggestion and now I am console logging [object Undefined]. Is this simply an unresolved promise or is there something else amiss here? Thanks.
can you console.log x before storing it in asyncstorage?
ahh yeah i'm seeing now it's sending an undefined object. The state is true when I check my checkbox, then I press store data and it fires true. The thing is if I don't try to stringify the object I get the following error: java.lang.Boolean cannot be cast to java.lang.String - node_modules/react-native/Libraries/Storage/AsyncStorage.js:347:24 in convertError
try to store like if(enableAuto){await AsyncStorage.setItem(STORAGE_KEY, 'true)} else {await AsyncStorage.setItem(STORAGE_KEY, 'false')}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.