0

check this image for better understading of question

enter image description here

this function only updates first index

function handleBooking(){
    
    const groundref = doc(db,'Owner', props.id)
   
    updateDoc(groundref,{
        slots: {Sat: [{available: book , status: status}]}
        } ).then(response => {
            alert("updated")
        }).catch(error =>{
            console.log(error.message)
        })  
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Does this answer your question? [Firestore Update single item in an array field](https://stackoverflow.com/questions/52187985/firestore-update-single-item-in-an-array-field), [How can I update an object inside of an array in firestore?](https://stackoverflow.com/questions/62417069/how-can-i-update-an-object-inside-of-an-array-in-firestore) – Doug Stevenson Aug 05 '23 at 12:55
  • Firestore doesn't have an operation to update the contents of a specific array item in a document. You have to read the document into memory, modify the array, then update the array field back into the document. – Doug Stevenson Aug 05 '23 at 12:56

1 Answers1

0

How to update a specific index of an array?

If the question is about Firestore, as also @DougStevenson mentioned in his comment, Firestore doesn't have the capability to update an array by a specific index. You have to read the document, perform the changes in the array, and then update the array back to Firestore.


If it's about the Realtime Database, then to be able to modify a node inside the Realtime Database, then you have to create a reference that points exactly to that node. So for example, if you want to change the value of the available field of the first element from false to true, and the slots node is a child of the root reference, then the following reference is needed:

db/slots/Sat/0/available
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193