react-native-firebase icon indicating copy to clipboard operation
react-native-firebase copied to clipboard

Returned records shows in reverse order even after using 'orderByKey()' and even with 'limitToFirst()'

Open weerakkodylr opened this issue 8 years ago • 8 comments

Issue

I'm developing a game with React Native & Firebase. I have changed the setup today to use React-Native-Firebase instead of Firebase for Web. import firebase from 'react-native-firebase'

After the changes I have noticed that data are displaying in reverse order.

e.g. const tournamentsData = firebase.database().ref('/Game/Tournaments') which earlier retrieved the tournaments array as [{Avengers: {...}}, {TomAndJerry: {...}}] but now it's coming as [{TomAndJerry: {...}}, {Avengers: {...}}]

I tried to use 'orderByKey()' then combined with 'limitToFirst(2)' then I tried "orderByChild('index')" but always result comes as same reverse order. When I check Objects within above objects are also received in reverse order

When I changed back to use firebase for web, everything back to correct order without any query changes. i.e. import firebase from 'firebase'

Environment

  1. Application Target Platform: Android 6 and above

  2. Development Operating System: Windows 8

  3. Build Tools: Android Studio 3.0.1 / Android SDK 25

  1. React Native version: 0.50.4
  1. RNFirebase Version: 11.6.2

weerakkodylr avatar Jan 30 '18 13:01 weerakkodylr

Hello thanks for the report. Could we see your code usage around making the database call.

Ehesp avatar Jan 31 '18 07:01 Ehesp

Hi Ehesp,

Please find it below. let me know if you need further information.

const tournamentsData = firebase.database().ref('/Game/Tournaments').orderByKey().limitToFirst(3)
 		tournamentsData.once('value')
 		.then((snapshot) => {
 			if(snapshot.val()) {
 				dispatch({
 					type: TOURNAMENT_DATA_LOADED,
 					payload: snapshot.val()
 				})
 			} else {
 				dispatch({
 					type: TOURNAMENT_DATA_LOADING_ERROR,
 				})
 			}
 		})

weerakkodylr avatar Jan 31 '18 09:01 weerakkodylr

@weerakkodylr how are you iterating over the returned payload out of interest? Firebase doesn't always give the data back in the order it is stored - a snapshot.forEach method is available to do this... Are you using it?

Ehesp avatar Jan 31 '18 09:01 Ehesp

@Ehesp , thanks for looking in to this.

Actually Firebase does return the data in the same order always (for me). The time I spent with firebase DB (past 6 months with web version in React Native) and this ordering issue didn't happen even a single time. firebase for Web always return the order I requested in the query and I didn't want to do additional ordering in the app.

And the other thing is then there is no use of 'orderByKey()' , 'orderByValue()'

I can ignore this issue by doing the ordering on the snapshot or inside the reducer but since I always receives the data from firebase as I expected, I believe this is some kind of an issue in the react-native-firebase and it should be fixed.

Thanks again for spending time on my request :)

weerakkodylr avatar Jan 31 '18 16:01 weerakkodylr

This issue has been automatically marked as stale because it has not had recent user activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 02 '18 01:09 stale[bot]

Closing this issue after a prolonged period of inactivity. If this is still present in the latest release, please feel free to create a new issue with up-to-date information.

stale[bot] avatar Sep 16 '18 01:09 stale[bot]

I'm having the similar issue. Not sure if anyone has already found a solution.

KennethSJiang avatar Sep 29 '18 13:09 KennethSJiang

I'm noticing this as well, adding orderByKey doesn't do anything. I wonder if this should be reopened?

dccarmo avatar Oct 25 '18 23:10 dccarmo