5

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, add few attributes and set the store. I get the store to obj as I have to compare in the next if condition.

  2. If the listobject entry already exists(2nd time), then, it directly comes to the 2nd block, and compares. (The reason I get values to obj in 1st step is because I can have a common obj.data.isdirty condition.

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 which are the outcome of the same issue-

  1. Logs. I am setting obj1 and getting the same value for obj (so that I can compare the next if condition). Why am I not able to get the same value that I have 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. This is the end result of the above logs. I am getting that list.data.isdirty is undefined. I guess that because the JSON format I am accessing does not exist in obj i.e., obj.data.isdirty doesn't exist. So, how do I overcome this?

undefined is not an object (evaluating 'list.data.isdirty');

Please tell me what am I doing wrong?

2
  • The obj returning is a string, so you have to apply JSON.parse to it before the second if. Commented Dec 3, 2015 at 6:10
  • 1
    I have tried that as well, its not working. Commented Dec 3, 2015 at 10:43

3 Answers 3

4

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

If I am right, should I be using obj as - obj.then(function(){code});
Yes. I used a normal object assign and it worked. Updated the question with my solution. Thanks :)
0

Sometimes while doing console.log(AsyncStorage.getItem('Soomekey')) you will be getting undefined as you can't directly pull values from the AsyncStorage as returns a promise so what you should be writing is

 const SomeFunction = async () => {
    try {
      const value = await AsyncStorage.getItem('somekey');
      console.log(value);
    } catch (err) {
      console.log(err);
    }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.