Angular 试图另存为对象数组,但一直保存为数组的数组

Angular 试图另存为对象数组,但一直保存为数组的数组,angular,ionic-framework,Angular,Ionic Framework,我正在尝试实现一个接口,以便更容易地将数据发布到我的数据库中。用户可以选择多个产品,当他们返回搜索更多产品时,页面将刷新,因此我必须将产品保存在本地存储中,直到他们签出 在我尝试实现该接口之前,每个附加产品都将作为对象存储在数组中。在尝试实现该接口之后,第一个产品是一个对象,其余产品是数组对象的数组 附加项 name: string = this.navParams.get('name'); desc: string = this.navParams.get('desc'); saveItem

我正在尝试实现一个接口,以便更容易地将数据发布到我的数据库中。用户可以选择多个产品,当他们返回搜索更多产品时,页面将刷新,因此我必须将产品保存在本地存储中,直到他们签出

在我尝试实现该接口之前,每个附加产品都将作为对象存储在数组中。在尝试实现该接口之后,第一个产品是一个对象,其余产品是数组对象的数组

附加项

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]);
});
}
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);
   });
}
附加项

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]);
});
}
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);
   });
}

保存(数据):承诺

我认为问题在于
产品。推送(数据)

相反,我会说你需要这样做

save(data): Promise<any> {

   return this.getData().then((products: any[]) => {
   if (products) {
     products = products.concat(data);
     return this.storage.set('products', products);
   }
   return this.storage.set('products', data);
   });
}
保存(数据):承诺{
返回此.getData()。然后((产品:any[])=>{
if(产品){
products=products.concat(数据);
返回此.storage.set('products',products);
}
返回此.storage.set('products',data);
});
}
此外,为了更好地进行类型检查,您可以将方法更改为

保存(数据:订单[]):承诺{