I have a simple item-calculator-app. The items are stored using Async Storage. Therefore I have created the following class itemStorage.js
import { AsyncStorage } from 'react-native';
export const saveItem = (item) => {
AsyncStorage.setItem(item.name, JSON.stringify(item));
}
export const getAllItems = async () => {
const result = [];
try {
await AsyncStorage.getAllKeys().then(async keys => {
await AsyncStorage.multiGet(keys).then(key => {
key.forEach(data => {
console.log(data)
result.push(data)//values
});
return result;
});
});
} catch (error) {
console.log('Error loading settings', error);
}
}
Saving the items works. I can also see the items in the console when I call console.log(data) in the getAllItems-function.
What does not work is the usage of this function in other components. I have a component called calculator.js and have the following code:
callbackFunction = async (childData) => {
const myItems = await getAllItems().then(item => {
console.log("items " + item);
});
But 'item' is always undefined. And I don't know what causes this behaviour.