如何使用 Angular 和 Cloud Firestore 创建和更新子集合?

How can I create and update a subcollection using Angular and Clound Firestore?

我在 Firestore 中创建了一个集合“Ticket”。我想当我更新数据时将在子集合中添加操作 'action' 但是没有像图片字段这样的数据 'actionSentence' 数据未输入

app.ts

  isSubmitAssignDev() {
    if (this.editTicket.controls.assign.value) {
      this.ticketService.setActionById(this.id, this.editTicket.controls.status.value, this.editTicket.controls.assign.value)
    }
  }
  setActionSentence() {
    if (this.status.value === 'Accepted') {
      this.editTicket.patchValue({
        actionSentence: 'MA accepted ticket'
    })
    } else if (this.status.value === 'Rejected') {
      this.editTicket.patchValue({
        actionSentence: 'MA rejected ticket'
      })
    } else if (this.status.value === 'Pending') {
      this.editTicket.patchValue({
        actionSentence: 'MA set pending'
      })
    } else if (this.status.value === 'Assigned') {
      this.editTicket.patchValue({
        actionSentence: 'Supervidor assigned ticket'
      })
    } else if (this.status.value === 'Resolved') {
      this.editTicket.patchValue({
        actionSentence: 'Dev resoled task'
      })
    }
  }

app.service.ts

  setActionById(id: any, status: any, staff: any, actionSEntence: any) {
    this.afs.collection('ticket').doc(id)
      .collection('action')
      .add({
        actionSEntence,
        staff,
        status,
        date: new Date(),
      })
  }

actionSEntence 没有添加到文档中,因为其中没有值,并且 Firestore 不会添加具有 Null 值的字段。您可以通过检查代码中的这一行来看到这一点:

this.ticketService.setActionById(this.id, this.editTicket.controls.status.value, this.editTicket.controls.assign.value)

你使用的是 3 个参数,而不是 4 个,所以由于 setActionById 的最后一个参数是 actionSEntence: any,它假定它是一个空值,this.afs...add({}); 将简单地忽略。

因此,为了修复它,您只需要在调用 this.ticketService.setActionById() 时设置第 4 个值。