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

230
Views
How to count the number of of children for a parent in the new firebase version 9 using numChildren()?

How to count the number of of children for a parent in the new firebase version 9 using numChildren()?

This is how it is done in firebase version 8 below

firebase.database().ref('users/' + userId).on('value', (snapData) => {
      console.log(snapData.numChildren())
    })

But in version 9 this isn't working below

   onValue(ref(db, 'users/'), (snapData) => {
      console.log(snapData.numChildren())
    })

Does anyone know how it is done in firebase version 9 numChildren()?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

There is no numChildren anymore in v9, but you can get the same value with"

onValue(ref(db, 'users/'), (snapData) => {
  console.log(Object.keys(snapData.val()).length) // 👈
})
about 4 years ago · Juan Pablo Isaza Report

0

Frank got it right, there is no numChildren anymore, but his answer produces an error if there is no value in the db

snapData.val() will be null and you can't Object.keys(null)

so it's:

onValue(ref(db, 'users/'), (snapData) => {
    const count = snapData.exists() && Object.keys(snapData.val()).length || 0;
})

Docs: https://firebase.google.com/docs/reference/js/database.datasnapshot

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!