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

167
Visualizações
How can I add data to a firestore array without overwriting it's previous content?

I'm trying to store book Ids in the books array in firestore. every time I add a new book it overwrites the first one.

await updateDoc(
  doc(dataBase, "users", user.uid),
  {
    currentlyReading: {
      arrayUnion(bookId)
    }
  },
  { merge: true }
);

I'm using updateDoc as stated in the documentation + merge: true, but it only stores one element. is there any way I can keep adding elements without overwriting? Thank you in advance.

this is a screen shot of the Document enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The { merge: true } prevents from existing fields from being removed when using setDoc() and doesn't work for array elements. There's no need for that option when using updateDoc(). One way would be to use arrayUnion() add new elements to the array.

import { doc, updateDoc, arrayUnion } from "firebase/firestore"

await updateDoc(doc(dataBase, "users", (user.uid)), {
  currentlyReading:{
    books: arrayUnion(bookId)
  }            
})
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