1

As stated in another question, I'm developing an Ionic 2 App using Firebase as Backend.

I have categories and I have products. Products belong to categories. As it is a "n to m" relationship, products and categories are stored in separate nodes in firebase. I structured the data as follows:

Firebase data structure:

Categories know, which products belong to them (keys are referenced in "prods" node of each category). Products know, which categories they belong to (keys are referenced in the "prod_cat" node). However, when I list all categories I just know the IDs of the products which belong to the category. In the template I need to show more details, like the product name for example.

I read many similar questions an came up with this solution to add the product information to the categories:

getProductsOfCategory(catId){
  this.productsOfCategory = this.af.database.list('/categories/'+catId+'/prods');

  return this.productsOfCategory
    .map(products => { 
      console.log(products); // works fine: an object containing the relevant product information is logged to the console (see screenshot)
      products.map( product => { console.log(product.$key); // works fine: the IDs of the products are logged to the console 
        product.prod_details = this.af.database.object('/products/'+product.$key); // seems not to work. Returned value: undefined
      });
    });

Unfortunately, this doesn't work. As written as comments in the code, the product information are collected correctly and logged to the console (see following screenshot): console screenshot

However, the returned object of the above function is "undefined".

When I try to state, that explicitely an object of type FirebaseListObservable is to be returned, I receive the error:

Type 'Observable' is not assignable to type 'FirebaseListObservable'. Property '$ref' is missing in type 'Observable'.

Does anybody have an idea what else I could try?

Thank you very much in advance!

Jane Dawson
  • 693
  • 2
  • 7
  • 19

1 Answers1

0

I solved the problem.

provider (*.ts):

getProductsOfCategory(catId){
  let result = this.af.database.list('/categories/'+catId+'/prods')
    .map(items => { 
      for (let product of items) { 
        this.af.database.object('/products/'+product.$key)
        .subscribe( data => {
          product.details = data;
        });
      }
      return items;        
    })      
  return result;
}


getCategoriesAndProducts(){
  let result = this.af.database.list('/categories')
    .map(items => { 
      for (let category of items) {
        this.getProductsOfCategory(category.$key)
        .subscribe(data => {
          category.productDetails = data;
        })
      }
      return items;
    })
  return result;
}

provider call in the tempaltes ts-file:

getCategoriesAndProducts(){
    this.categoryService.getCategoriesAndProducts()
      .subscribe(data => {
          this.categoriesAndProducts = data;
});

template / view (*.html):

<ng-container *ngFor="let cat of categories">
  <ng-container *ngIf="cat.parent_cat === 'noParentCategory'">
    <h3>
      {{cat.cat_name}}
    </h3>
    <ng-container *ngFor="let subcat of categoriesAndProducts">
      <ion-list>
        <ng-container *ngIf="subcat.parent_cat === cat.$key">            
          <ion-item-divider>
            {{subcat.cat_name}}
          </ion-item-divider>
          <ion-item *ngFor="let prod of subcat.productDetails">
            {{prod.details?.prod_name}}
          </ion-item>
        </ng-container>
      </ion-list>
    </ng-container>
  </ng-container>
</ng-container>
Jane Dawson
  • 693
  • 2
  • 7
  • 19