简体   繁体   English

试图将 api 响应存储在异步存储中,但我无法存储它

[英]trying to store api responses in asyncstorage but I am unable to store it

here I have pasted code which is written class component for mobx state management and i am calling it to another file,the thing is I have above 450 api responses and it need to store locally so i have tried it but i am not getting any value nor the data stored in database pls help me out thanks in advance..在这里,我粘贴了编写 class 组件的代码,用于 mobx state 管理,我将其调用到另一个文件,问题是我有超过 450 个 api 响应,所以我不需要在本地获取任何值,但我已经尝试过它在本地存储响应也不是存储在数据库中的数据请帮助我提前谢谢..

class ProductStore {
  constructor() {
    makeAutoObservable(this);
  }

  screenWidth = width;

  screenHeight = height;

  headerHeight = 0;

  isiOS = Platform.OS === 'ios';

  isAndroid = Platform.OS === 'android';

  isProductLoading = 'pending';

  productData = [];

  filterdData = [];

  search = '';

  isFlatlistRender = false;

  setFields(eName, data) {
    this[eName] = data;
    console.log(eName, data);
  }

  
  getproductData = () => {
    if (this.isProductLoading == 'loading') {
      return true;
    }
    this.isProductLoading = 'loading';
    this.productData = [];

    let headers = new Headers();
    headers.set(
      'Authorization',
      'Basic ' + encode('username:password'),
    );

    fetch('some_url', {
      method: 'GET',
      headers: headers,
    })
      .then(response => response.json())
      .then(responseJson => {
        console.log('.....', responseJson);
        AsyncStorage.setItem(
          'ACCESS_TOKEN',
          JSON.stringify(responseJson),
          err => {
            if (err) {
              console.log('an error');
              throw err;
            }
            console.log('success');
          },
        ).catch(err => {
          console.log('error is: ' + err);
        });
        try {
          const value = AsyncStorage.getItem('ACCESS_TOKEN');
          if (value !== null) {
            console.log(JSON.parse(value));
          }
        } catch (error) {}
        this.productData = responseJson;
        this.isProductLoading = 'done';
      })
      .catch(error => {
        console.error(error);
        this.isProductLoading = 'error';
      });
  };
}
export default new ProductStore();

AsyncStorage.getItem() returns a promise, not the value. AsyncStorage.getItem() 返回 promise,而不是值。 So, just add a then block after the line AsyncStorage.getItem('ACCESS_TOKEN');因此,只需在AsyncStorage.getItem('ACCESS_TOKEN');行之后添加一个then. . It would be like this会是这样

AsyncStorage.getItem('ACCESS_TOKEN').then(value => {
          if (value !== null) {
            console.log(JSON.parse(value));
          }
}).catch(err => console.error(err));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试将复选框存储到异步存储 - Trying to store checkbox to asyncstorage 试图将 object 存储到 AsyncStorage 但它存储空值 - Trying to store object into AsyncStorage but it stores empty values 我正在尝试将经度和纬度保存为猫鼬模型,但无法存储它们 - I am trying to save latitude and longitude to mongoose model but I am unable to store them 我是 redux 的新手并做出反应,我正在尝试更新 REDUX 商店中的嵌套 state,但无法对其进行整理 - i am new to redux and react i am trying to update the nested state in REDUX store, but unable to sort it out 如何将东西存储在异步存储中? - How to store things in asyncstorage? 我想通过AsyncStorage展览将存储持久化到Redux,但出现错误 - I want to persist the store to redux with expo AsyncStorage but i get error 当我想要存储数据时反应本机 AsyncStorage 错误 - React Native AsyncStorage Error when I want store data 如果复选框为真,我如何使用 asyncstorage 存储? - How can i store if checkbox is true using asyncstorage? 如何在 React Native 中使用 asyncstorage 存储唯一数据? - How can I store unique data with asyncstorage in React Native? 我正在尝试将我的函数调用存储在一个对象中 - i am trying to store my function call within an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM