React Native AsyncStorage: Cannot resolve promise returned by getItem

不羁岁月 提交于 2019-12-14 02:12:10

问题


I have the following code that should return an item from AsyncStorage.

However the item is never read:

const key = 'shoppingListItems';

export default class ShoppingListService {
    static async getItems() 
    {
        let result = await AsyncStorage.getItem(key);

        return result;
    }

    // ...
}

And I use it in a component (screen):

// ...

  componentDidMount()
  {
    alert(JSON.stringify(ShoppingListService.getItems()));
  }

// ...

It always shows me a message with:

{"_40":0,"_65":0,"_55":null,"_72":null}

How do i get the data inside AsyncStorage?


回答1:


  async componentDidMount()
  {
    alert(JSON.stringify(await ShoppingListService.getItems()));
  }

I made the componentDidMount function async. I dont know if thats recommended, but this works.



来源:https://stackoverflow.com/questions/49701344/react-native-asyncstorage-cannot-resolve-promise-returned-by-getitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!