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

334
Visualizações
How to update a specific field of data only one time for each user in firesrore in firebase v9?

I am creating a sns-like web application, and trying to implement a "liking a post" function. Basically each post has data in firestore, such as the caption of the post, the image of the post, number of likes on the post and etc. What I want to do is, if a user clicks a like button on any post, the number of likes on the post clicked will be incremented by 1. However, I do not know how I can restricting each user can like only one time on each post. My current code works for incrementing the number of likes but the user can like a post as many times as the user clicks. I am totally a beginner in firebase, react and javascript and would be really appreciated for any help. Thank you.

here is my code working for incrementing a number of likes.

 const likeHandle = async () => {
        const docRef = doc(db, "posts", postId);

        await updateDoc(docRef, {
            noLikes: noLikes + 1
        });
    }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You won't be able to do this at scale inside the same document where you are tracking the number of likes, because there is a max size to each document.

What you will have to do instead is store a record of each pair of user and like in its own document in a separate collection, and check to make sure that any new like requests are not already represented in that other collection. Your UI should also probably disable likes if there is a record for that pair of user and post so that the user is not presented with an option that you don't want them to take.

about 4 years ago · Juan Pablo Isaza Relatório

0

There is no direct way to limit how often a user can write a specific piece of data.

What you can do however is:

  • Add a usersWhoVoted field with UIDs to the document, or a userWhoVoted subcollection under it.
  • Write both the increased number of likes and the UID to the database in one operation (using a transaction/batched write if you chose a subcollection).
  • Use security rules that only allow the user to increment the count if they also added their UID in that operation, and only decrease the count if they removed it (if that is also a use-case you want to support).

You should probably also use the atomic increment operation, to prevent depending on client-side state that can be outdated (although your security rules should catch and reject that situation anyway).

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