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

133
Visualizações
How to add previous data with new one in firebase realtime database javascript
function add(){
  firebase.database().ref("user/l6xetRv3ddhjlerJm10d3aYkF3w1" ).update({            
    points: "2000"
  })
}

I am using this code above to update a specified user points for test but I want to update points of currently logged in user. I tried doing it on function onAuthStateChanged block, but it was unsuccessful. Any suggestions how should I do it?

And is it possible to update previous points adding with current update points? for example:- the previous points of user is 2000, the update function updates 2000 points but i want it to update previous points + current points which is goona be update (2000+2000).

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

0

To write the points of the currently sign in user:

const uid = firebase.auth().currentUser.uid;
firebase.database().ref("user").child(uid).update({            
  points: "2000"
})

To increment/decrement the points of the user, you can use the atomic increment operator:

const uid = firebase.auth().currentUser.uid;
firebase.database().ref("user").child(uid).update({            
  points: firebase.database.ServerValue.increment(2000);
})

Note that this will only work if you've stored the points as a number though, so:

firebase.database().ref("user").child(uid).update({            
  points: 2000 // 👈 without quotes
})

And then

firebase.database().ref("user").child(uid).update({            
  points: firebase.database.ServerValue.increment(2000);
})
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