首页 > 解决方案 > 如何删除(或添加)作为 Firebase Firestore 云文档一部分的数组内的对象?

问题描述

我在 Firestore 中有一个集合(患者),其中包含多个文档(每个患者),每个文档都有一个自动生成的 ID,在我的 Web 应用程序中我显示了一个特定文档,该文档是我使用角度路由器的 ActivatedRoute 从其 ID 获得的。

在我的服务中,我可以使用以下代码通过 id 访问一个单独的“Patient”:

export class FirebaseService {
  private patientCollection: AngularFirestoreCollection<Ipatients>;

  constructor(private afs: AngularFirestore) {
    this.patientCollection = afs.collection<Ipatients>("Patient", (ref) =>
      ref.orderBy("Name")
    );
  }

 public getOnePatient(id: Ipatients): Observable<Ipatients> {
    return this.afs.doc<Ipatients>(`Patient/${id}`).valueChanges();
  }

这是我的 component.ts,我在 ngOnInit 中请求单个患者(按 id)

constructor(
    private fbservice: FirebaseService,
    private route: ActivatedRoute
  ) {
    this.route.params.subscribe((params) => {
      console.log(params["id"]);
    });
  }

  ngOnInit() {
    const idPatient = this.route.snapshot.params.id;
    this.fbpatient$ = this.fbservice.getOnePatient(idPatient);
  }
  

在 HTML 中,我只渲染部分患者信息,我渲染的部分是文档中称为会话的数组,这些是我希望用户与之交互的会话,因此我循环遍历患者内部的会话数组和用 ngFor 显示里面的每个对象

显示 Firestore 中的患者结构的图像

这是患者数据的示例:

 fbpatient: Ipatients = 
    {
      id: "asdSDWESHGawhadah",
      Nombre: "John",
      Apellido: "Doe",
      Edad: "30",
      Telefono: "098849625",
      Mutualista: "SMI",
      Sesiones: [         // THIS IS THE ARRAY I WANT TO C.R.U.D 
        {
          nombre: "Sesion1",
          ordenesEntregadas: 1,
          fechaSesion: new Date(2019, 11, 24),
          asistencia: "Asistio",
          conAviso: null,
          datosCompletos: true,
        },
        {
          nombre: "Sesion2",
          ordenesEntregadas: 1,
          fechaSesion: new Date(2019, 11, 24),
          asistencia: "Asistio",
          conAviso: null,
          datosCompletos: true,
        },
      ],
    }

我的问题是如何在患者体内删除(或添加)“会话”,我的服务需要什么代码?我找不到这种情况的任何例子,只有整个文档的 CRUD

标签: javascriptangularfirebasegoogle-cloud-firestore

解决方案


推荐阅读