Ionic 2: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays

后端 未结 1 1974
说谎
说谎 2021-01-28 12:01

I am trying to retrieve data from firebase in my service class using:

return this.http.get(\'https://myfirstfirebaseproject-6da6c.firebaseio.com/products.json\')         


        
相关标签:
1条回答
  • 2021-01-28 12:40

    Please check your JSON. https://myfirstfirebaseproject-6da6c.firebaseio.com/products.json It returns an Object not an Array. That is why you cannot use returned data in ngFor.

    {
        -KiTWPuI_TYt0b_Qi03Y: {
        amount: 0,
        category: "Baby",
        content: "I love cricket",
        date: "2017-03-01",
        title: "Cricket"
        },
        -Kid7fghtlxkyrOChQPk: {
        amount: "111",
        category: "Book",
        content: "updated content",
        date: "2017-04-01",
        title: "Cycling"
        },
        d9e7545c-90a3-4a57-97ab-ea3bf9171023: {
        amount: "12",
        category: "Baby",
        content: "COnten1",
        date: "2017-01-01",
        title: "Title2"
        }
    }
    

    Try this:

    <ion-card ion-item *ngFor="let key of keys; let i = index" (click)="onItemClick(key)">
        <ion-card-header>
          {{ products[key].date | date }}
        </ion-card-header>
        <ion-card-content>
          <ion-card-title>
            {{ products[key].title }}
          </ion-card-title>
          <p>
            {{ products[key].content }}
          </p>
        </ion-card-content>
      </ion-card>
    

    In your component.ts:

    keys: string[];
    ...
    this.productService.fetchProducts()
                .subscribe(data => {
                  console.log(data)
                  this.products = data;
                  this.keys = Object.keys(this.products);
                });
    

    And modify your onItemClick() accordingly.

    0 讨论(0)
提交回复
热议问题