Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!