React-native-firebase: orderByChild once('value') not working

Created on 23 Apr 2017  路  8Comments  路  Source: invertase/react-native-firebase

First of all, thanks for this great project!

I have this issue with orderByChild which is not working as expected.

   firebase.database().ref(`/users/${userUid}/items`)
      .orderByChild('priority')
      .once('value', snapshot => {
        // doesn't sort
      });  

similar to this issue from react-native-firestack
https://github.com/fullstackreact/react-native-firestack/issues/82

Anyone else with the same issue?

Most helpful comment

Please see: https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#forEach

Because of the way JavaScript objects work, the ordering of data in the JavaScript object returned by val() is not guaranteed to match the ordering on the server nor the ordering of child_added events. That is where forEach() comes in handy. It guarantees the children of a DataSnapshot will be iterated in their query order.

All 8 comments

Hey, when you say doesn't work in the callback, does it simply not get there or something else?

The returned result is not in proper order.

Are you iterating over the results using snapshot.forEach?

I am using a lodash map on the result. But the snapshot.val() is not in order even before the _.map

Please see: https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#forEach

Because of the way JavaScript objects work, the ordering of data in the JavaScript object returned by val() is not guaranteed to match the ordering on the server nor the ordering of child_added events. That is where forEach() comes in handy. It guarantees the children of a DataSnapshot will be iterated in their query order.

That's good to know. Let me look into that. Thanks!

Closing for now, let me know if you have anymore problems with the above taken into account.

@Ehesp I tried what you pointing towards and it is give error;

getDates() {
    firebase.database().ref('dates').orderByKey().once('value').then(
      (snapshot) => {
        snapshot.forEach(date => {
          let dt = date.snapshot()
          this.dates.push(dt);
        });
        console.log(this.dates);
       }
    )
  }

TypeError: snapshot.forEach is not a function

I am using Angular+NativeScript.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ODelibalta picture ODelibalta  路  3Comments

alizahid picture alizahid  路  3Comments

dgruseck picture dgruseck  路  3Comments

joecaraccio picture joecaraccio  路  3Comments

mahyarr picture mahyarr  路  3Comments