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

108
Visualizações
firestore updateDoc replaces entire field instead of updating?

Im trying to figure what I am doing incorrectly here when I want to update just a value of my field in the firestore but instead the entire field got replaced?

also for whatever reason my date variable seems to directly become the string date in firestore instead of the value it holds?

Here's my code for updating the Doc:

export async function addLikesToFeed(date, likes){
    try{
        // it literally updates to {'date': 'likes: 1'}
        await updateDoc(doc(db, 'Bulletin', `bulletin`), { date: {likes: likes+1}})
    } catch(error){
        console.log('error updating Likes at addLikesToFeed: ', error.message)
    }
}

The below is a screen shot of my firestore. If I hard code '05-02-2022' in the date part, it becomes like what you see in the image. If I kept date as seen in the code, it becomes the date like part.

the date variable holds a string and likes is a number.

I want my function to work so that when I click on my button it will add one like to the value in the likes part, and I am trying to figure out why I cant do that

Thanks

enter image description here

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

0

If you want to use the value of date as the field name, you can use [] notation:

await updateDoc(doc(db, 'Bulletin', `bulletin`), { [date]: {likes: likes+1}})

This will still replace the entire field for that date though. If you want to update only the likes subfield, you can use dot notation to update fields in nested objects:

await updateDoc(doc(db, 'Bulletin', `bulletin`), { [date+".likes"]: likes+1})

Finally, Firestore actually has a build in operator for incrementing values, which prevents race conditions when multiple users are updated the likes values at almost the same time.

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