3

I want to get all users from a Firebase realtime database and sort them by a score property. I've got this to work using the variable
users: FirebaseListObservable<any[]>;
but I get the errors:

Type 'Observable<any[]>' is not assignable to type 'FirebaseListObservable<any[]>'.
Property '_ref' is missing in type 'Observable<any[]>'.

If I change the variable to
users: Observable<any[]>;
then the errors go away, but then I can't use the Angularfire methods like .push() and .update().

My fetching and sorting code (which works) looks like:

this.users = this.af.database.list('/users')
  .map(items => items.sort((a, b) => b.score - a.score));

I would appreciate any advice on how this should be done to avoid the errors, or any preferred way, thank you!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Lindsay Ward
  • 469
  • 4
  • 11

2 Answers2

11

The FirebaseListObservable implements lift, so RxJS operators - like map - will return a FirebaseListObservable instance.

It's just that - when using TypeScript - the RxJS operators are statically typed to return Observable, so you have to cast to FirebaseListObservable. (Using RxJS does not have to involve TypeScript and if you look at the guidance for adding operators you will see that types are not mentioned.)

You can verify that a FirebaseListObservable instance is returned by looking for the push and update functions you mentioned in your question:

this.users = this.af.database
  .list('/users')
  .map(items => items.sort((a, b) => b.score - a.score)) as FirebaseListObservable<any[]>;
console.log(this.users.push.toString());
console.log(this.users.update.toString());

Note, however, that this will not work for FirebaseListObservable instances that are created as queries. There is an as-yet-unanswered question regarding such instances.

Community
  • 1
  • 1
cartant
  • 57,105
  • 17
  • 163
  • 197
  • 1
    @LindsayWard This is done on the client side. For large set of data, you might want to sort on the server side. Angularfire2 support a 2nd parameter in `list()` for sort by. – Sam Sep 08 '16 at 22:22
  • Thanks @Sam. Good to know. I'll try that as well and see if I notice any difference. – Lindsay Ward Sep 11 '16 at 04:26
  • 2
    I get the following error: `[ts] Property 'map' does not exist on type 'FirebaseListObservable'.` Any ideas please? I am using: `"angularfire2": "^2.0.0-beta.7-pre",` and `"firebase": "^3.6.4",` More details: http://stackoverflow.com/questions/41550132/property-filter-does-not-exist-on-type-firebaselistobservable – Richard Jan 09 '17 at 17:47
  • @sam i added the sortBy in the query parameter. how can i specify the sorting - desc or asc? – alltej Mar 07 '17 at 13:34
  • @Sam, can i use any sort function in the 2nd parameter in list()? Can you please see this quesion https://stackoverflow.com/q/50112214/4299527 ? – Setu Kumar Basak May 01 '18 at 08:55
2

You need this example. To watch other opts see Query interface.

this.items = angularFire.database.list('/users', {
  query: {
    orderByChild: 'childPropertyName'
  }
});

export interface Query {
    [key: string]: any;
    orderByKey?: boolean | Observable<boolean>;
    orderByPriority?: boolean | Observable<boolean>;
    orderByChild?: string | Observable<string>;
    orderByValue?: boolean | Observable<boolean>;
    equalTo?: any | Observable<any>;
    startAt?: any | Observable<any>;
    endAt?: any | Observable<any>;
    limitToFirst?: number | Observable<number>;
    limitToLast?: number | Observable<number>;
}
  • Error: `Argument of type '{ query: { orderByChild: string; }; }' is not assignable to parameter of type 'QueryFn'. Object literal may only specify known properties, and 'query' does not exist in type 'QueryFn'` – Dumbo Dec 26 '19 at 12:35