AsyncStorage.getItem returns undefined: React Native

Code -

I am checking if an entry named listobject exists in AsyncStorage.

  • If it does not exist, I create an object, add several attributes and install the repository. I get obj storage as I need to compare the following if condition .

  • If the listobject entry already exists (second time), then it goes directly to the second block and is compared. (The reason I get the values ​​for obj in the first step is because I can have the general condition obj.data.isdirty .

Here is my code -

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 = AsyncStorage.getItem('listobject');
        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();

I have 2 questions that are the result of the same problem -

  • Logs. obj1 obj ( if). , set?

12-03 00: 27: 56.281 32598-487/com.abc D/ReactNativeJS: 'obj1 = { "data": { "isdirty": true}}'

12-03 00: 27: 56.286 32598-487/com.abc D/ReactNativeJS: 'obj = { "_37": 0, "_ 12": null, "_ 59": []}'

  1. . , list.data.isdirty - undefined. , JSON, , obj, .. Obj.data.isdirty . , ?

undefined ( "list.data.isdirty" );

, , ?

+4
2

. .

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();
+4

, AsyncStorage. , Async , . , getItem (), , Promise, , .

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

obj .

, obj isDirty child, Promise.

0

Source: https://habr.com/ru/post/1618326/


All Articles