Update array value with IONIC Storage

Asked

Viewed 196 times

0

I am using Storage to store my order data as per the image below: inserir a descrição da imagem aqui

I need to check if the request (numped) exists before saving, if it exists update its data, if not I add new request. However my problem is that in checking, it is overwriting everything, example I have 2 requests, while saving, it leaves only 1, in case what I saved.

Follows my code:

import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
import { HttpHeaders, HttpClient } from '@angular/common/http';

let syncURLOrder = 'http://200.98.129.191:8080/dashboard/Webservice/integrationMobile/';

@Injectable()
export class OrdersProvider {
    items = [];

    constructor(private http: HttpClient, private storage: Storage) {}

    // key == 'orders'
    public save(key: string, orders: Orders) {
        this.storage.get(key).then((data) => {

            if(data == null) {
                data = [];
            }

            let result = data.find(filtro => {
                return filtro.numped === orders.numped;
            });

            // if exists upd new data ==> problem
            if(result) {
                this.items = [orders];
            } else {
                // if not exists, get current orders and add new order
                this.items = data;
                this.items.push(orders);
            }

            return this.storage.set(key, this.items);
        });
    }

    public remove(key: string) {
        return this.storage.remove(key);
    }

    public getAll() {
        let orders = [];

        return this.storage.get('orders').then((data) => {
            if(data != null) {
                for(let value of data) {
                     orders.push(value)
                }
                orders.reverse();
            } else {
                orders = [];
            }
        }).then(() => {
            return Promise.resolve(orders);
        }).catch((error) => {
            return Promise.reject(error);
        });
    }

    public getLastOrder() {
        let lastOrder = 0;
        let orders    = [];

        return this.storage.get('orders').then((data) => {
            if(data != null) {
                for(let value of data) {
                    orders.push(value)
                }
                orders.reverse();
                if(orders == null || orders == undefined) {
                    lastOrder = 0;
                } else {
                    lastOrder = orders[0].numped;
                }
            } else {
                lastOrder = 0;
            }
        }).then(() => {
            return Promise.resolve(lastOrder);
        }).catch((error) => {
            return Promise.reject(error);
        });
    }

    public getOrder(numped: number) {
        let newData = [];

        return this.storage.get('orders').then(data => {
            let result = data.find(filtro => {
                return filtro.numped === numped;
            });

            newData.push(result);
        }).then(() => {
            return Promise.resolve(newData);
        }).catch((error) => {
            return Promise.reject(error);
        });
    }
}

export class Orders {
    numped: number;
    pedsis: number;
    seqped: number;
    codcli: number;
    conpag: number;
    nompag: string;
    codven: number;
    datlan: string;
    datemi: string;
    prefat: string;
    totpro: number;
    totdes: number;
    totped: number;
    basipi: number;
    valipi: number;
    valfre: number;
    valost: number;
    observ: string;
    status: number;
    nomven: string;
    nomcli: string;
    totitm: number;
    itens: any[];
    sincro: string;
}
  • The guy Ordens returns the object of Order or an array ?? If it is an array, then the call orders.numped I think you are wrong, since you would have to go through the array.

  • It returns the object of Orders, I will put my complete code of the Provider

  • Try to put this.items = [...data, orders]; in Else.

1 answer

1


At first if you place only one item in the array and save that 1 item array in the Storage.

Instead, try to find the Index of the object in the array of data and modifies that object.

let index = data.findIndex(filtro => {
    return filtro.numped === orders.numped;
});

if (index !== -1) {
    data[index] = [orders];
}

...

return this.storage.set(key, data);
  • Okay, I tested it here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.