AsyncStorage.getItem 返回 undefined :React Native [英] AsyncStorage.getItem returns undefined : React Native
问题描述
代码流是-
我正在检查 AsyncStorage 中是否存在名为 listobject 的条目.
如果它不存在,那么我创建一个对象,添加一些属性并设置存储.我将商店送到 obj,因为我必须在下一个 if 条件下进行比较.
如果listobject条目已经存在(第二次),那么,它直接来到第二块,并进行比较.(我在第一步中为 obj 获取值的原因是因为我可以有一个常见的 obj.data.isdirty 条件.
这是我的代码-
AsyncStorage.getItem('listobject').then((obj) => {如果(对象 == 未定义){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));}如果(obj.data.isdirty){obj.data.isdirty = false;AsyncStorage.setItem('listobject',JSON.stringify(obj));return AsyncStorage.getItem('listobject');}}).完毕();
我有 2 个问题是同一问题的结果-
- 日志.我正在设置 obj1 并为 obj 获取相同的值(以便我可以比较下一个 if 条件).为什么我无法获得与设置相同的值?
<块引用>
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":[]}'
- 这是上述日志的最终结果.我得到 list.data.isdirty 未定义.我想这是因为 obj 中不存在我正在访问的 JSON 格式,即 obj.data.isdirty 不存在.那么,我该如何克服这个问题?
<块引用>
undefined 不是一个对象(评估 'list.data.isdirty');
请告诉我我做错了什么?
我实际上是将对象从一个复制到另一个.成功了.
AsyncStorage.getItem('listobject').then((obj) => {如果(对象 == 未定义){var obj1 ={};obj1.data ={};obj1.data.isdirty = true;console.log("obj1 = "+ JSON.stringify(obj1));AsyncStorage.setItem('listobject',obj1);obj = obj1;//这就是我所做的!console.log("obj = "+ JSON.stringify(obj));}如果(obj.data.isdirty){obj.data.isdirty = false;AsyncStorage.setItem('listobject',JSON.stringify(obj));return AsyncStorage.getItem('listobject');}}).完毕();
Codeflow is-
I am checking if an entry called listobject exists in the AsyncStorage.
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.
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-
- 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":[]}'
- 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?
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();
这篇关于AsyncStorage.getItem 返回 undefined :React Native的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!