0

I have a database substructure where I keep a count of the number of reports assigned to each admin

The database substructure looks like this

ReportCount
       uuid string1: count1
       uuid string2: count2
       uuid string3: count3

I wish to retrieve this information using

firebase.database().ref('ReportCount').orderByValue().on("value", <Some Function>)

I want to do this to be able to figure out which admin has the least number of reports so a new report can be assigned to that admin. However, when I use orderByValue, I see that the data retrieved is not ordered in ascending nor descending order of count values.

What is the issue here?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • The Firebase Realtime Database and Cloud Firestore are two separate databases. Please only mark your question with the relevant tag, not with both. – Frank van Puffelen Jul 07 '21 at 04:12
  • Please edit your question to show: 1) the code that handles the data, as the problem may well be in there. 2) the actual JSON as text, instead of the schematic display you have now. You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jul 07 '21 at 04:14

1 Answers1

0

If you need to get the admin with least number of tasks on hand then you can just use the limitToFirstmethod:

firebase.database().ref("/ReportCount").orderByValue().limitToFirst(1).once("value").then((snapshot) => {
  console.log(user.key, user.val())
})

If you are fetching all the admins and the data is not ordered, that is your browser as answered here.

If you run a loop directly on a snapshot as shown, the data will be logged in the order:

firebase.database().ref("/ReportCount").orderByValue().once("value").then((snapshot) => {
  snapshot.forEach((user) => {
    console.log(user.key, user.val())
  })
})
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84