0

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 :)

1
  • this.db.list this returns Observables? Commented Jun 7, 2017 at 6:38

1 Answer 1

3

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();
              });
});
Sign up to request clarification or add additional context in comments.

3 Comments

I tried to implement your solution, but I couldnt see any products in my html page
in your .ts file, use the following code to print the array at first, to check if the array exists: this.products.subscribe((items)=>console.log(items)) you can add this code in ngOnInit() method
my bad, had a typing mistake in my HTML. It works now :) thank you for the answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.