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

220
Visualizações
Increment/Decrement Function with Firebase Realtime Database using vuejs

Hi everyone I have some difficulties increment à number in firestore by using a simple button in Vue js, does someone help?

the goals are: *increase and decrease the value of the number through the firestore by using the Counter collection *hide the button to prevent the spam of it

I also done it but did'nt work:

<html>
<HelloWorld msg="Welcome to Your Vue.js App"/>
    <span id="countspan">pleas wait...</span>
    <button id="incBtn" disabled>Increase</button>
    <button id="decBtn" disabled>Decrease</button>
</html>

<script>

// Initialize Firebase

const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);


let span = document.getElementById('countSpan');
let inc = document.getElementById('incBtn');
let dec = document.getElementById('decBtn');
let countVariable;

window.onload = function(){
  const dbRef=ref(db);

  get(child(dbRef, 'Counter')).then((snapshot)=>{
    countVariable = Number(snapshot.val());

    if(this.id=='incBtn') countVariable++;
    else countVariable--;

    update(ref(db,"/"),{Counter: countVariable});

    span.innerHTML = countVariable;
    setTimeout(BtnEnable,500);
  });
}

inc.addEventListener('click',IncDecCounter);
dec.addEventListener('click',IncDecCounter);


function IncDecCounter(){
  BtnDisable();

  const dbRef= ref(db);

  get(child(dbRef, 'Counter')).then((snapshot)=>{
    countVariable = Number(snapshot.val());
    span.innerHTML = countVariable;
    BtnEnable();
  });
}

function BtnEnable(){
  inc.disabled=false;
  dec.disabled=false;
}

  function BtnDisable(){
  inc.disabled=true;
  dec.disabled=true;
}

</script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To increment the counter in the database, you can use Firebase's atomic increment operator:

import { ... , increment } from 'firebase/database';

// ...

update(dbRef, { 'Counter': increment(1) });

Decrementing is done as an increment with a negative value:

update(dbRef, { 'Counter': increment(-1) });
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