How to update() an array of maps-objects on Firestore in Angular or Angularfirestore?

落爺英雄遲暮 提交于 2021-01-28 13:01:02

问题


I know how to READ and WRITE a document in Firestore, But how does one update() a Value of and object in an array of Maps without removing/deleteing? Just changing one value if needed.

I have a Form that includes a FormArray, that are able to edit as an when there is an update on the price list.

product = {
  id: "product id",
  brand: "Brand Name",
  model: "This is the model of the brand"
  lists: [
    { location: "Location 1", price: "0.00" },
    { location: "Location 2", price: "0.00" }
  ]
}

Component.ts

    export class PageComponent implements OnInit {
      productDoc: AngularFirestoreDocument<Product>;
      product: Observable<any>;
    }

constructor(private afs: AngularFirestore) {}

  update() {
    this.productDoc.update({ amount: this.updatedProduct });
  }

Appreciate to shine some light on it, Thank You!


回答1:


There is no way to change a value in an array. You'll either have to remove the old combination of values and add the new combination, or have to:

  1. Read the document.
  2. Get the entire array from that document.
  3. Modify the array in your application code.
  4. Write the modified array back to the database in its entirety.


来源:https://stackoverflow.com/questions/63679460/how-to-update-an-array-of-maps-objects-on-firestore-in-angular-or-angularfires

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!