0

I'm creating a react app and i'm using firebase/firestore for the database. I want to have a textarea that updates the name of an object inside of an array in the firestore db.

The structure of the db looks like this: enter image description here

I want to update the name property of one of the objects in the "categories" array.

The code I've written so far looks like this:

    const updateCategoryInfo = async (categoryId: number, categoryName: string) => {
      await updateDoc(docRef, {
        [`categories[${categoryId}].name`]: categoryName
      })
      .then(() => {
        console.log("Category updated succesfully!")
      })
      .catch((error) => {
        console.error("Error updating categoryname", error)
      })
    }

The code gives this error:

enter image description here

Kron
  • 17
  • 4
  • 1
    What value holds the `categoryId` variable? – Alex Mamo Jul 28 '23 at 12:02
  • @AlexMamo It holds a number between 0-5 to choose which object in the "categories" array should be updated. There are 5 different textareas and the number gets determined based on which textarea you type in – Kron Jul 28 '23 at 12:19
  • 1
    You can't target specific array elements for update in Firestore. You have to read the document, modify the array in memory, then write the updated field back to the document. – Doug Stevenson Jul 28 '23 at 12:48

0 Answers0