Ionic 3 存储装置并变得奇怪

巴尼K

我有一个 Ionic 3 应用程序,我在其中存储了 SQLite 中的对象数组。

我的 this.data 数组(在下一节中解释)如下所示:

[
    {
        guid: "xy",
        images: [
            { guid: 0, uploaded: true },
            { guid: 1, uploaded: true }
        ],
    },
    {
        guid: "xz",
        images: [
            { guid: 0, uploaded: false },
            { guid: 1, uploaded: false }
        ],
    }
]

所以一个对象又具有一个 guid 和一个对象数组。更新项目后,我想将所有项目保存到存储以及上传器类的变量中。上传器类有一个 this.data 和 this.key 属性。

这是有问题的部分的片段:

updateItem(value){
    // search for the index of the item in the array
    var index = this.data.findIndex((item) => {
        return item.guid == value.guid;
    });

    if(index >= 0){
        // update the item in the array with the changed one
        this.data[index] = value;

        // this works fine, prints with the updated item
        console.log(this.data);

        // it supposed to save the whole array with the changed items
        return this.storage.set(this.key, this.data).then(() => {

            // for debug I read the data again - same happens after app restart
            this.storage.get(this.key).then((data) => {
                // this logs the initial, unchanged items
                console.log(data);
            });
        });
    }

    return Promise.reject(new Error('error'));
}

首先,它搜索this.data数组中项目的索引,如果找到,则覆盖数组中的项目。然后它尝试将其保存到存储中。出于调试目的,我读取了存储和 console.log。

将“xz”对象的图像设置为 后uploaded = true,我调用updateItem(secondItem).

从第一个console.log我看到“xy”和“xy”对象的图像都上传了:true。storage.set 被调用,在 storage.get 中,初始状态发生。“xy”对象的图像已上传:true,但“xz”对象的图像为false。重新启动我的应用程序后,此状态再次加载。

如果 this.data 中只有一个对象,updateItem 可以正常工作,例如我将存储设置为上传:false,然后更改属性,调用updateItem(firstItem),它保存上传状态。但如果数组中有多个对象,则只保存一个。

我尝试将其保存为 JSON,并在我读回时进行解析,但结果是相同的。

巴尼K

我最终克隆了数组,然后保存了克隆,然后将克隆分配给了原始数组。这解决了我的问题。

updateItem(value){
    // search for the index of the item in the array
    var index = this.data.findIndex((item) => {
        return item.guid == value.guid;
    });

    var newData = this.data.slice();

    if(index >= 0){
        // update the item in the array with the changed one
        newData[index] = value;

        // it supposed to save the whole array with the changed items
        return this.storage.set(this.key, newData).then(() => {
            this.data = newData;

            // for debug I read the data again - same happens after app restart
            this.storage.get(this.key).then((data) => {
                // this logs the initial, unchanged items
                console.log(data);
            });
        });
    }

    return Promise.reject(new Error('error'));
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Ionic 3 本地存储中的数组

来自分类Dev

如何使用 *ngFor 指令从 Ionic 3(Cordova、Ionic 3、Angular 5)上的存储中获取数据

来自分类Dev

ionic 3 在函数ionViewDidLoad()中读取存储值本机存储

来自分类Dev

模拟 removeEventListener ionic 3

来自分类Dev

奇怪的IONIC错误消息

来自分类Dev

Ionic 3 电子书

来自分类Dev

如何从 Ionic 3 上传图片?

来自分类Dev

ionic 3 movefile 函数的问题

来自分类Dev

数组项过滤 Ionic 3

来自分类Dev

Ionic 3 中的事件问题

来自分类Dev

如何将 ionic 3 空白提交到我的 GitHub 存储库?

来自分类Dev

Ionic3 - 如何与存储提供程序实现持久聊天系统?

来自分类Dev

sqlite 存储中数据的动态键值(Ionic 3、Cordova、Angular 5)

来自分类Dev

如何将数据数组对象保存到离子 SQlite 存储(TypeScript、Angular 5、Ionic 3)

来自分类Dev

如何添加带有ionic / ionic 2 / ionic 3 / ionic 4 / ionic 5的本地cordova插件?

来自分类Dev

将Ionic 3迁移到Ionic 5 API

来自分类Dev

Ionic 2 / Ionic 3 / Ionic 4:(懒惰)正在加载微调器以获取图片

来自分类Dev

Ionic 3 侧边菜单标题中心

来自分类Dev

JSON 解析中的 Ionic 3 错误

来自分类Dev

ionic 3 动画同时显示隐藏

来自分类Dev

Ionic3 页面之间的参数

来自分类Dev

Ionic 3 - 防止浏览器关闭

来自分类Dev

Ionic 3 - 导航或弹出 N 个页面

来自分类Dev

Ionic 3 Firebase 登录因错误而停止

来自分类Dev

带有ionic 3的HTTP Post

来自分类Dev

样式按钮警报 - Ionic3

来自分类Dev

Ionic 3:img 下的灰色(?)单杠

来自分类Dev

来自 JSON 数组的 Ionic 3 navPush

来自分类Dev

Ionic 3 UnhandledPromiseRejectionWarning:未处理的承诺拒绝

Related 相关文章

热门标签

归档