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

225
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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