1

I want every object under each date under an expense category, in the Firebase Database.

In Firebase:

- expenses
      - food
          - 07-11-2016 //"I want every object in here"
              + -KX7Aju4_G6i6pcNoBzj

In my view I've tried:

       <div *ngFor="let date of food | async">

          <ion-item-divider sticky>
            {{date.$key}}
          </ion-item-divider>

          <div *ngFor="let item of date"> //This don't seem to work
            <ion-item>
              <h3>{{item.title}}</h3>
              <p>
                {{item.amount}} Kr &mdash;
                kl. {{item.time}}
              </p>
            </ion-item>
          </div>

        </div>

FIREBASE WARNING: Exception was thrown by user callback. Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

Clearly it gives me the date as an object, not an array.

I've also tried retrieving the values in JSON, but iterating the JSON object with *ngFor (with out the pipe async) also gives me a bunch of problems.

  /* Returns a Firebase list object as a JSON object */
  getAsJSON(firebaseList: any){
    firebaseList
      .map(obj => obj.map(obj => {
        return obj;
      }));
  }

I'm trying to iterate the firebase list object "food".

this.food = af.database.list('/expenses/food');

OR

this.food_json = getAsJSON(this.food);

I've read and tried a bunch, but I can't seem to find the right solution.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Kevin Frostad
  • 181
  • 1
  • 1
  • 12
  • For you second ngFor you need to transform object to array, use pipe for that. Good answer is here: http://stackoverflow.com/questions/35647365/how-to-display-json-object-using-ngfor/35647396#35647396 – Yevgen Nov 22 '16 at 10:42
  • Thank you @Yevgen will look in to it. – Kevin Frostad Nov 22 '16 at 13:02

1 Answers1

0
import {Component} from '@angular/core';
import {AngularFire, FirebaseListObservable} from 'angularfire2';

@Component({
  selector: 'app-root',
  template: `
  <ul>
    <li *ngFor="let item of items | async">
       {{ item | json }}
    </li>
  </ul>
  `,
})
export class AppComponent {
  items: FirebaseListObservable<any>;
  constructor(af: AngularFire) {
    this.items = af.database.list('/items');
  }
}

retrieving-data-as-lists

ksav
  • 20,015
  • 6
  • 46
  • 66