i have, a data in firebase. the structure look like this
what i want is, to count all child inside a uid. so, i expect the values return 4
i have tried like this
JavaScript
x
4
1
database.ref('data').on('value', (snap) => {
2
console.log(snap.numChildren())
3
})
4
that return 2
how do i count all children in all uid?. so i can get 4
Advertisement
Answer
You can run a forEach loop on the snapshot and use numChildren()
on all the child nodes.
JavaScript
1
10
10
1
database.ref('data').on('value', (snap) => {
2
let total = 0
3
snap.forEach(node => {
4
console.log(node.val())
5
total += node.numChildren()
6
})
7
8
console.log(total)
9
})
10