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

144
Visualizações
Unable to get doc id in Firebase

I am trying to get the doc id of each data entry upon click to delete that specific record, but upon checking it is only showing id of the first entry made in the Firebase.

const deleteRecord = () => {
  db.collection("records").onSnapshot((querySnapshot) => {
    querySnapshot.forEach((doc) => {
      // console.log(doc.id)
      let recToDel = document.querySelectorAll(".records")
      for (let toDelRecord of recToDel) {
        toDelRecord.onclick = () => {
          console.log(doc.id)
        }
      }
    })
  })
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The loops are nested, so the last iteration of the querySnapshot.forEach loop is the one that sets the same doc.id for every recToDel dom element.

Fix by looping just one collection and indexing into the other...

    let recToDel = Array.from(document.querySelectorAll(".records"));
    querySnapshot.docs.forEach((doc, index) => {
      if (index < recToDel.length) {
        let toDelRecord = recToDel[index];
        toDelRecord.onclick = () => {
          console.log(doc.id)
        }
      }
    });

If there are fewer .records elements than there are docs, some won't be assigned.

Doing this onSnapshot will cause these assignments to be made every time the collection changes, including on deletes. If that's not your intention, fix by changing to get().

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