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

164
Visualizações
firebase - add and remove new values into an array

I have two array in firebase realtime database that will manage user followers and following. I've created this function in my vue app that will add the follow if an user click on the button in the profile of another user and the following into the profile of the user who clicked:

        async followUser() {
            await update(ref(db, 'Users/'+ this.profileKey), {
                followers: [store.currentUser.uid]
            })
            await update(ref(db, 'Users/'+ store.currentUserKey), {
                following: [this.$route.params.uid]
            })
        }

At the moment I've tested on two profile I've created ad-hoc and the function act as expected but I have a doubt about it. If the user will start follow other users, will the new entries will be added correctly into the existing array or all the data will be overwritten? What's the correct way to push or remove valuse into an array stored in firebase realtime database?

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

0

There is no atomic way to add an item to or remove an item from an array in the Firebase Realtime Database API. You'll have to read the entire array, add the item, and write the array back.

Better would be to store the followers and followings as maps instead of arrays, with values of true (since you can't have a key without a value in Firebase. If you'd do that, adding a new user follower would be:

set(ref(db, 'Users/'+ this.profileKey+'/followers/'+store.currentUser.uid), true)

And removing a follow would be:

set(ref(db, 'Users/'+ this.profileKey+'/followers/'+store.currentUser.uid), null)

Or

remote(ref(db, 'Users/'+ this.profileKey+'/followers/'+store.currentUser.uid))

Also see: Best Practices: Arrays in Firebase.

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