反应本机 AsyncStorage 错误响应

React native AsyncStorage bad response

已创建 React Native 应用程序并需要使用 AsyncStorage 以从其存储机制中获益。

要保存在 AsyncStorage 中,请使用代码:

  _storeData = async (param) => {
    try {
      let par = JSON.stringify(param);
      //await AsyncStorage.setItem(this.key, par);
      Utilities.setItem(this.key, par);
      this._retrieveData();
    } catch (error) {
      console.log(JSON.stringify(error));
    }
  };

检索数据:

 _retrieveData = async () => {
    try {      
      const value = Utilities.getItem(this.key);
      if (value !== null) {
        alert('data is new: ' + JSON.stringify(value));
      }
    } catch (error) {
    }
  };

并且,在 Utilities 部分中设置 Item 和 getItem:

const setItem = (key, value) => {
  if (!key || !value) return;
  AsyncStorage.setItem(key, value);
};

const getItem = (key) => {
  if (!key) return;
  var val = AsyncStorage.getItem(key);
  return val;
};

正在保存数据,但我得到的响应看起来不正确,因为它是 'weird' 个字符的字符串:

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

有人知道我为什么会得到这样的答案吗?

请注意 AsyncStorage.getItem 也是异步的 - 奇怪的字符代表 getItem 返回的承诺。

使用var val = await AsyncStorage.getItem(key);并将你的getItem效用函数标记为async。您还需要 await 调用 Utilities.getItemUtilities.setItem