0
I am using Storage to store my order data as per the image below:
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 ofOrder
or an array ?? If it is an array, then the callorders.numped
I think you are wrong, since you would have to go through the array.– Victor Henrique
It returns the object of Orders, I will put my complete code of the Provider
– wribeiro
Try to put
this.items = [...data, orders];
in Else.– Victor Henrique