Sean Letendre
Sean Letendre

Reputation: 2883

Why can I not call toArray() on FirebaseListObservable?

In an Ionic Project, I have:

import { AngularFireDatabase, FirebaseListObservable} from 'angularfire2/database';

and a class with the field:

songs: FirebaseListObservable<any>;

therefore, the line of code:

this.songs = db.list('/songs');

works and allows me to put the line:

<button ion-button ouline *ngFor="let song of songs | async">

in my html without problem.

Now, FirebaseListObservable extends the RxJS Observable (source). Furthermore, Observable has a method toArray(). But, when I run my project, I see:

Typescript Error
Property 'toArray' does not exist on type 'FirebaseListObservable<any>'.

Why is this? Is there another way I can get an array from what songs is observing?

Upvotes: 1

Views: 759

Answers (1)

nisanarz
nisanarz

Reputation: 927

I don't really sure why the toArray() not working , but i can suggest you a way to get the array you want from the DB.( I usually do that when i want just the array without the ability to listen to any changes of the DB - like Observable does) :

this.db.list('/songs')
.first().toPromise()
.then(response => {
  //code to handle the response - in this case its a list

  This.items = response;
})
.catch(error => { //error code here });

dont forget to import the rxjs first and toPromise

I really hope it fits your wish and helps you :)

Upvotes: 1

Related Questions