AsyncStorage.getItem returns undefined : React Native

后端 未结 2 1913
闹比i
闹比i 2021-01-06 07:46

Codeflow is-

I am checking if an entry called listobject exists in the AsyncStorage.

  1. If it doesn\'t exist, then, I create an object,

相关标签:
2条回答
  • 2021-01-06 08:12

    I'm not quite following the entire question I do however see an issue with the use AsyncStorage. Going by the name, Async implies that the operations are asynchronous. So when you do getItem(key), you should either pass in a callback or use the Promise object it returns as you are doing in the first line of code.

    obj = AsyncStorage.getItem('listobject'); console.log("obj = "+ JSON.stringify(obj));

    obj is going to be the Promise in this case.

    Then if you check on obj for the presence of a data and isDirty child property, they will not exist on the Promise.

    0 讨论(0)
  • 2021-01-06 08:22

    I actually copied the object from one to another. It worked.

    AsyncStorage.getItem('listobject').then((obj) => {
        if(obj == undefined)
        {
            var obj1 ={};
            obj1.data ={};
            obj1.data.isdirty = true;
            console.log("obj1 = "+ JSON.stringify(obj1));
            AsyncStorage.setItem('listobject',obj1);
            obj = obj1; //THIS IS WHAT I DID!
            console.log("obj = "+ JSON.stringify(obj));
        }
        if(obj.data.isdirty)
        {
            obj.data.isdirty = false;
            AsyncStorage.setItem('listobject',JSON.stringify(obj));
            return AsyncStorage.getItem('listobject');
        }
    }).done();
    
    0 讨论(0)
提交回复
热议问题