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

3
  • 1
    What value holds the categoryId variable? Commented Jul 28, 2023 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 Commented Jul 28, 2023 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. Commented Jul 28, 2023 at 12:48

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.