最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I update an object inside of an array in firestore? - Stack Overflow

programmeradmin2浏览0评论

I would like to update the pleted property of an object in an array in Firestore, but I have no idea how to reach that specific element in the array. The image will show the structure.

I have e up this far but don't know how to choose, for example, item 1 in the array. I was thinking of using its ID (it has an id property) but don't know how to get there.

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

By the way, this is how the array was created (and how other objects are pushed to it)

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

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

I would like to update the pleted property of an object in an array in Firestore, but I have no idea how to reach that specific element in the array. The image will show the structure.

I have e up this far but don't know how to choose, for example, item 1 in the array. I was thinking of using its ID (it has an id property) but don't know how to get there.

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

By the way, this is how the array was created (and how other objects are pushed to it)

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

businessRef.update({
        [`bookings.${currentDate}`]: firebase.firestore.FieldValue.arrayUnion(bookingObj),
      })
Share Improve this question edited Jun 16, 2020 at 20:44 uber asked Jun 16, 2020 at 20:29 uberuber 5,1136 gold badges33 silver badges65 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

Firestore does not have an operation that allows you to update an existing item in an array by its index.

To update an existing item in the array, you will need to:

  1. Read the entire document into your application.
  2. Modify the item in the array in your application code.
  3. Write back the entire array to the document.

I'm pretty sure this has been asked before, so let me see if there's an answer with an example.

Also see:

  • How to remove an array element according to an especific key number?
  • Simple task list ordering - how to save it to Firebase Firestore?
  • How to update only a single value in an array
  • How to update an "array of objects" with Firestore?
发布评论

评论列表(0)

  1. 暂无评论