【问题标题】:How can I create and update a subcollection using Angular and Clound Firestore?如何使用 Angular 和 Cloud Firestore 创建和更新子集合?
【发布时间】:2021-03-12 04:23:39
【问题描述】:

我在 Firestore 中创建了一个集合“票证”。我想当我更新数据时会在子集合'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(),
      })
  }

【问题讨论】:

    标签: angular google-cloud-firestore angular8


    【解决方案1】:

    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 个值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 2021-02-20
      • 2021-12-09
      • 2018-05-31
      相关资源
      最近更新 更多