14

In my React Native app's AsyncStorage, I have multiple JSON objects, each with a unique key id like so:

 '62834456':
 data: { "foo": "bar",
 "nicknames": [ "grizz", "example" ],
 ... and so on }

They have been pushed into AsyncStorage stringified. I'm trying to retrieve every object by their id, and push both the id and its' JSON data into the component's state. I have this so far:

// for every key in async storage, push to favorites in state
importData = (id) => {
  for (id in AsyncStorage) {
    return AsyncStorage.getItem(id)
      .then(req => JSON.parse(req))
      .then(json => console.log(json))
      .catch(error => console.log('error!'));
  }
}

When console.logging 'json' in the above function, the result is null. How can I properly access all JSON objects in my AsyncStorage?

FINAL EDIT

Using your code example and removing JSON.parse (so simply console.logging req) returns this: enter image description here

It appears this is happening because for some reason .forEach is returning the first string in the array, the array itself, then the second string.

6 Answers 6

25

In order to get all AsyncStorage keys, you need to call AsyncStorage.getAllKeys(). In order to speed things up, you should also use AsyncStorage.multiGet() By doing that your code becomes;

importData = async () => {
  try {
    const keys = await AsyncStorage.getAllKeys();
    const result = await AsyncStorage.multiGet(keys);

    return result.map(req => JSON.parse(req)).forEach(console.log);
  } catch (error) {
    console.error(error)
  }
}
Sign up to request clarification or add additional context in comments.

8 Comments

So it looks like id isn't being using in this case, do I need to define it somewhere before passing it into importData()?
Key is the id here, you can use normal getItem as keys is just an id array and connect them that way.
Thank you for your help. At the moment, I am having trouble getting things to display in the console after parsing.. i.e. In your example, if I console.log(req), nothing will display, not even a generic object response, the console is blank. Is this not the proper way to view these objects after being parsed?
Could you check what is in keys? Perhaps there isn't anything set yet.
Why mix async / await with then and callbacks? Why not go the whole way as in @georgekpc 's answer?
|
6

here is a more elegant way to get all items using async/await functions

const fetchAllItems = async () => {
    try {
        const keys = await AsyncStorage.getAllKeys()
        const items = await AsyncStorage.multiGet(keys)

        return items
    } catch (error) {
        console.log(error, "problemo")
    }
}

Comments

4

May be, little straight forward using promises.

import { AsyncStorage } from 'react-native';

  AsyncStorage.getAllKeys()
    .then((keys)=> AsyncStorage.multiGet(keys)
                    .then((data) => console.log(data)));

Cheers!

Comments

1

Something that I didn't prefer about the current answers is that it returns back an array of arrays which will make it harder to search for a specific key. Here is what I prefer to use:

const getAll = async () => {
  try {
    const result: any = {};
    const keys = await AsyncStorage.getAllKeys();
    for (const key of keys) {
      const val = await AsyncStorage.getItem(key);
      result[key] = val;
    }
    return result;
  } catch (error) {
    alert(error);
  }
};

This will return back an object with all the keys you have and their respective values.

Comments

0

Code for return a json object

    try {
        const keys = await AsyncStorage.getAllKeys()
        const itemsArray = await AsyncStorage.multiGet(keys)
        let object = {}
        itemsArray.map(item => {
          object[`${item[0]}`] = item[1]
        })
        return object
    }   catch (error) {
        console.log(error, 'error')
    }

Comments

0

By using Object.fromEntries (doc)

const getAllData = async() => {
  try {
    const keys = await AsyncStorage.getAllKeys();
    const result = await AsyncStorage.multiGet(keys);
    const obj = Object.fromEntries(result);
    // obj be like {"key1": "value 1", "key2": "value 2", .....}
    // Now parse the 
    Object.keys(obj).forEach(key => {
      obj[key] = JSON.parse(obj[key]);
    });
  } catch (error) {
    console.error(error);
  }
};

Comments

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.