Quantcast
Channel: Ionic Forum - Latest posts
Viewing all articles
Browse latest Browse all 229724

Trying to save as Object Array but keeps getting saved as Array of Array

$
0
0

I am trying to implement an interface to make it easier to post data to my database. A user can select multiple products and when they go back to search for more products the page is refreshed so I have to keep the products in local storage until they checkout.

Before I tried to implement the interface each additional product would just get stored as an object inside the array. After trying to implement the interface the first product is an object and the rest are array of array objects

AddItem.ts

 name: string = this.navParams.get('name');
 desc: string = this.navParams.get('desc');

saveItem() {

    let newItem = {
      name: this.name,
      desc: this.desc
    };

    this.dataStorageService.save(newItem).then(() => {
      this.navCtrl.pop(Search);
    });
}

DataStorageService.ts

save(data): Promise<any> {
return this.getData().then((products: any[]) => {
  if (products) {
    products.push(data);
    return this.storage.set('products', products);
  }
  return this.storage.set('products', [data]);
});
}

How the objects are getting stored

Here is what I'm trying to do now

Order.Interface.ts

export interface Order {

  name: string;
  desc: string;
}

AddItem.ts

name: string = this.navParams.get('name');
desc: string = this.navParams.get('desc');
orders = [] as Order[];

saveItem(): void {

this.orders.push({name: this.name, desc: this.desc});

this.dataStorageService.save(this.orders).then(() => {

});

DataStorageService.ts

save(data): Promise<any> {

   return this.getData().then((products: any[]) => {
   if (products) {
     products.push(data);
     return this.storage.set('products', products);
   }
   return this.storage.set('products', data);
});

How its getting stored


Viewing all articles
Browse latest Browse all 229724

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>