【发布时间】:2018-01-20 03:43:51
【问题描述】:
我有一个 Ionic 应用程序,我在其中存储了一个带有 Native-Storage 的 Array。 这个Array是一个对象数组。
我是这样存储的:
>>> array1:CertainType[] 至少有 50 个混洗元素
this.nativeStorage.setItem('array1', { property: array1 })
.then(
() => { console.log('array1 stored') },
error => { console.log('array1 not Stored',error)
});
我这样检索项目:
this.nativeStorage.getItem('array1').then(
array1 => {
//On the Sucess of getting Array1 use it to create Array2
array2 = array1.splice(0,5); //<-- MY PROBLEM IS HERE
},
error => {
console.error('Error Getting Array', error);
}
);
我不断收到
的错误我认为是因为存储和检索的过程弄乱了数组的类型等。
我试着做演员:
..array1 as CertainType[]
--已编辑>> 我尝试了字符串化和 JSON 解析。
this.nativeStorage.setItem('array1', { property: JSON.stringify(array1)}).then(. . . .
array2 = JSON.parse(array1);
抛出这个错误:
ERROR Error: Uncaught (in promise): SyntaxError: Unexpected token o in JSON at position 1
SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
但我在 splice() 上不断收到同样的错误。
如果我不使用存储逻辑,代码运行得很好......任何线索。我错过了什么? :/
【问题讨论】: