【问题标题】:React Native AsyncStorage: Cannot resolve promise returned by getItemReact Native AsyncStorage:无法解析 getItem 返回的承诺
【发布时间】:2018-09-16 23:41:02
【问题描述】:

我有以下代码应该从 AsyncStorage 返回一个项目。

但是该项目从未被读取:

const key = 'shoppingListItems';

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

        return result;
    }

    // ...
}

我在一个组件(屏幕)中使用它:

// ...

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

// ...

它总是向我显示一条消息:

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

如何获取 AsyncStorage 中的数据?

【问题讨论】:

  • 您是否处于 Android 开发模式?
  • 我使用的是iOS模拟器

标签: reactjs react-native asyncstorage


【解决方案1】:
  async componentDidMount()
  {
    alert(JSON.stringify(await ShoppingListService.getItems()));
  }

我创建了componentDidMount 函数async。我不知道这是否推荐,但这有效。

【讨论】:

  • 是的,问题是您没有使用 async/await 或使用 AsyncStorage.getItem('itemKey').then(result => {})
猜你喜欢
  • 2017-01-11
  • 2019-11-23
  • 2019-01-30
  • 2018-04-14
  • 1970-01-01
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
相关资源
最近更新 更多