如何在Firestore中更新数组内的对象?

时间:2020-06-16 20:29:59

标签: javascript reactjs google-cloud-firestore

我想更新Firestore中数组中对象的completed属性,但是我不知道如何到达数组中的特定元素。该图像将显示结构。

enter image description here

我已经走了这么远,但是不知道如何选择,例如数组中的第1项。我当时在考虑使用其ID(它具有id属性),但不知道如何到达那里。

const businessRef = db.collection('approvedBusinesses').doc(businessId)
      try {
        businessRef.update({
          [`bookings.${currentDate} ????? `]: true // what to add after currentDate?
        })

顺便说一下,这就是数组的创建方式(以及其他对象如何推入数组)

const bookingObj = {
      carro: 'PASSA_CARRO',
      completed: false,
      userId: userObject.uid,
    }

businessRef.update({
        [`bookings.${currentDate}`]: firebase.firestore.FieldValue.arrayUnion(bookingObj),
      })

1 个答案:

答案 0 :(得分:2)

Firestore没有允许您通过其索引更新数组中现有项目的操作。

要更新阵列中的现有项目,您将需要:

  1. 将整个文档读入您的应用程序。
  2. 在应用程序代码中修改数组中的项目。
  3. 将整个数组写回到文档中。

我很确定以前已经问过这个问题,所以让我看看是否有一个例子的答案。

另请参阅:

相关问题