Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
How to update array inside an array in Google Cloud Firestore?

Parent array gets removed when updating the array inside an array. I only want the nested array to get updated. How to solve it? below is my code:

    const handleSubmit = async (e) => {
    e.preventDefault();
    
    const commentToAdd = {
      displayName: user.displayName,
      photoURL: user.photoURL,
      content: newComment,
      createdAt: timestamp.fromDate(new Date()),
      id: Math.random()
    }
    
    await updateDocument(project.id, {
      tasks: [ {
        comments: [{...project.tasks.comments, commentToAdd}],
      }
        
      ]
    })
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Firestore doesn't support updating array elements at given index. You first need to fetch the document, update array field with relevant data and update the whole array back. Nested arrays will make it even more complex to update a nested item.

You can simply this a bit by creating a sub collection for tasks as shown below:

projects -> { projectId } -> tasks -> { taskId }

This way each task document will have a comments array. However, you'll still have to read the comments and then write updated array back.

about 4 years ago · Juan Pablo Isaza Relatório

0

Like @Dharmaraj said, Firestore doesn't support updating array elements at given index.

As far as I understand your question. If you want that parent Array not to be removed, and just update/add commentsToAdd in the comments array. You could also use setDoc as opposed to updateDoc. See below code for example.

for (let step = 0; step < 5; step++) {
    const commentToAdd = {
        displayName: `someName-${step}`,
        photoURL: `someURL-${step}`,
        content: `someContent-${step}`,
        createdAt: new Date(),
        id: Math.random()
      };

    await setDoc({someRef}, {
        tasks: {
            comments: arrayUnion(commentToAdd)
        }
    }, {merge: true})
  }

The code above would result to: enter image description here

I looped the code to push multiple comments on the field. Just a note, by doing this, you're field types will be updated accordingly:

 - tasks: `map`
 - comments: `array`
 - commentFields: `map`

You may also refer to this documentation for more information.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda