React Native AsyncStorage: Cannot resolve promise

2019-07-18 10:58发布

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条回答
太酷不给撩
2楼-- · 2019-07-18 11:14
  async componentDidMount()
  {
    alert(JSON.stringify(await ShoppingListService.getItems()));
  }

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

查看更多
登录 后发表回答