Christer
Christer

Reputation: 3076

Ionic 2 Angularfire2 - add list of objects from database to FirebaseListObservable

I'm trying to display a list of objects in my Firebase Database, but I am having a hard time to add the objects to the FirebaseListObservable.

Here is my code:

    products: FirebaseListObservable<any[]>;

    this.db.list('products/' + this.user.uid, { preserveSnapshot: true})
                .subscribe(snapshots=>{
                  var jsonObject = [];
                  snapshots.forEach(snapshot => {
                    jsonObject.push(snapshot.val());
                  });
                  this.products = jsonObject;
    });

The error I get is that I can't assigne any[] to a FirebaseListObservable.

This is my HTML:

<ion-list>
    <ion-item class="text" *ngFor="let product of products | async">
      {{product}}
    </ion-item>
 </ion-list>

My database looks like this:

Thanks in advanced for the help :)

Upvotes: 0

Views: 637

Answers (1)

Allen Chen
Allen Chen

Reputation: 70

your products is FirebaseListObservable, however, your jsonObject is an array, so you can't let this.products = jsonObject

maybe you should do like the following:

products: Observable<any[]>;
this.products = this.db.list('products/' + this.user.uid, { preserveSnapshot: true})
            .map(snapshots=>{
             return snapshots.map(snapshot => {
                return snapshot.val();
              });
});

Upvotes: 3

Related Questions