簡體   English   中英

Angular 6 firebase數據庫在我的結構中有多個子鍵

[英]Angular 6 firebase database having multiple child key like in my structure

在我的Firebase數據庫結構中,我需要將此發貨表中的產品檢索到我的Angular 6 HTML表格格式中,我只訪問發貨數據,但我無法訪問產品子數據。 它說錯誤:找不到'對象'類型的不同支持對象'[object Object]'。 NgFor僅支持綁定到諸如Arrays之類的Iterables。

**Shippment.service.TS:**

    getshippings() {
      this.shippings = this.db.list("shippings");
       return this.shippings;
     }

**UserComponent.TS:**

    export class UserComponent implements OnInit {
      [x: string]: any;

      shippinglist : Billing[];
      user:User;
      shipping: Billing;

      constructor(public authService: AuthService,
        private shippingService:ShippingService) {}

      ngOnInit() {
        this.getAllProducts();
      }

      getAllProducts() {
    const x = this.shippingService.getshippings();
    x.snapshotChanges().subscribe(
      product =>  {
        this.shippinglist = [];
        product.forEach(element =>  {
          const y = element.payload.toJSON();
          y["$key"] = element.key;
          this.shippinglist.push(y as Billing);
        });
      }
    );
  }
    }

** Billing.TS:**

    export class Billing {
           $key: string;
           firstName: string;
           lastName: string;
           emailId: string;
           address: string;
           landmark: string;
           country: string;
           state: string;
           zip: string;
           city:string;
           phone:number;
           products : [{
              $key?: string;
              productId: number;
              title: string;
              category: string;
              price: number;
              descr: string;
              imageUrl: string;
         }]
      }

**HTML:**

    <table class="table">
        <thead>
            <tr>
                <th>Name</th>
                <th> Address </th>
                <th> Email ID </th>
            </tr>
        </thead>
        <tbody>
            <tr *ngFor="let p of shippinglist">
                <td>{{p.firstName}} {{p.lastName}}</td>
                <td>{{ p.address }}, {{p.landmark}},{{p.city}}, {{p.state}}, 
                     {{p.country}},{{p.zip}}</td>
                <td>{{ p.emailId }}</td>
                <ng-container *ngFor="let m of p.products">
                   <td>{{m.dscr}}</td>
               </ng-container>
            </tr>
        </tbody>


       </table>

 I **expect output** to get also product information in my table: ![Firebase Database][1][Output Screen][2]
[1]: https://i.stack.imgur.com/BfHW0.jpg

要訪問*ngFor的嵌套列表,您需要執行類似的操作,

<div *ngFor="let p of shippinglist">

    <div *ngFor="let m of p.products">

        <span>{{m.descr}}</span>

    </div>

</div>

重要的是,您從p.products接收p.products作為對象,而不是列表,因此您需要修改getAllProducts

getAllProducts() {
    const x = this.shippingService.getshippings();

    x.snapshotChanges().subscribe(
      response => {

        response.forEach(element => {

          const product = element.payload.toJSON();

          let all_product = product as Billing;

          all_product.products = product["products"];

          let new_product_arr = [];

          for (var p in product["products"] as object) {
            let push_products = product["products"][p] as Products;
            new_product_arr.push(push_products);
          }

          all_product.products = new_product_arr;

          this.shippinglist.push(all_product);
        });
      }
    );
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM