Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I use AsyncStorage.getItem() to retrieve the value of a specified key, it returns a Promise object as String not as an Object

Stored objected is:

{
 name: 'Chris',
 age: 30,
 traits: {
    hair: 'brown',
    eyes: 'brown'
  },
};

not able to retrieve value.propertyName.

AsyncStorage.getItem('key').then( (value) => console.log('value ', value, 'name is ', value.name));

output is : value {"name":"Chris","age":30,"traits":{"hair":"brown","eyes":"brown"}} name is undefined.

Please let me know how to retrieve a specific value from promise object

like image 484
Shailaja Avatar asked May 29 '17 16:05

Shailaja


People also ask

What does AsyncStorage getItem return?

getItem ​ Gets a string value for given key . This function can either return a string value for existing key or return null otherwise. In order to store object value, you need to deserialize it, e.g. using JSON. parse() .

What is AsyncStorage and how do you use it?

AsyncStorage is an unencrypted, asynchronous, persistent, key-value storage system that is global to the app. It should be used instead of LocalStorage. It is recommended that you use an abstraction on top of AsyncStorage instead of AsyncStorage directly for anything more than light usage since it operates globally.

How get value from AsyncStorage in React Native?

To get value from asyncstorage, React native asyncstorage provide getItem() method, it will expect storage key and return value . Check below examples to get string, number, or object in AsyncStorage. To get number in AsyncStorage we have to first convert it to number using parseInt() method.

How does AsyncStorage store data?

Saving the data This method is going to be promise-based, which means you can use the async await syntax with a try-catch block. Passing the identifier STORAGE_KEY as well as the input state variable, the API method AsyncStorage. setItem is going to store the value in storage.


2 Answers

AsyncStorage saves data only as strings. You just need to use JSON.stringify() when saving and JSON.parse() when retrieving.

For instance:

AsyncStorage.getItem('key')
.then((value) => {
  const data = JSON.parse(value);
  console.log('name is ', data.name);
});

Another option is to use some kind of wrapper for AsyncStorage which does this for you, like: https://github.com/jasonmerino/react-native-simple-store

like image 84
Samuli Hakoniemi Avatar answered Nov 02 '22 06:11

Samuli Hakoniemi


You could do for set the value the following

    async setAsyncStorage (key, value) {
        try {
          await AsyncStorage.setItem(key, JSON.stringify(value));
        } catch (error) {
          console.warn(error);
        }
      }

And for get the following:

    async getAsyncStorage (key) {
        try {
          const getAsyncStorageData = await AsyncStorage.getItem(key);
          const getAsyncStorageParsed = JSON.parse(getAsyncStorageData);
          return getAsyncStorageParsed;
        } catch (error) {
          console.warn(error);
        }
      }
like image 45
Marcos Ordieres Avatar answered Nov 02 '22 06:11

Marcos Ordieres