0

I am having an issue with ordering using @react-native-firebase/database. The database looks like this below:

users: {
  user_id: {
    rooms: {
      room_id_1: {
        name: "Tom"
        meta: {
          lastMessage: 1579702466378
          unreadCount: 0
        }
      },
      room_id_2: {
        name: "Jerry"
        meta: {
          lastMessage: 1579702467000
          unreadCount: 5
        }
      }
    }
  }
}

Here is my query. I am expecting the results to be ordered by the 'unreadCount' key:

import firebaseDB from '@react-native-firebase/database';

firebaseDB().ref(`Users/user_id/rooms`)
  .orderByChild(`/meta/unreadCount`)
  .on('value', (dataSnapshot) => {
    dataSnapshot.forEach((child) => {
      console.log('child.key', child.key)
    });
  });

But looks like orderByChild order isn't being respected. The response is always:

room_id_1
room_id_2

what can be the issue here?

Ankit
  • 21
  • 6
  • 1
    Given that `room_id_1` has a `unreadCount` of `0`, and `room_id_2` has an `unreadCount` of `5`, that result seems expected to me. – Frank van Puffelen Jan 22 '20 at 15:26
  • Yes, thanks @FrankvanPuffelen. I think I completely misunderstood it. It works fine for me now. The order is always ASC, I have handled the DESC case locally. – Ankit Jan 22 '20 at 20:15

0 Answers0