类型'undefined []'无法转换为类型'OrderArray'。类型“ undefined []”中缺少属性“ menuName”

时间:2018-10-24 12:32:42

标签: angular ionic-framework

目标

您好,我正在创建一个餐厅订购应用。我能够从firebase读取可观察的列表。现在,我想对每个菜单使用ion-select来分配某人要订购的数量。

order menu

问题

现在,我只想将订单的数量分配给模型。我已经创建了一个名为 OrderArray 的菜单订单模型,并且我希望将此模型作为 Array 。这是模型order-array.ts

export interface OrderArray {
  $key?: string,
  menuName: string,
  menuPrice: string,
  quantity: string
}

然后,我想将此数组转换为字符串,并将其分配给另一个名为 Order 的模型。这是模型order.ts

export interface Order {
  custName: string,
  custPhone: string,
  restoName: string,
  restoPhone: string,
  restoAddress: string,
  message: string,
  orderArray: string
 // timestamp: number
}

问题是在我加载页面时出现的:

  

类型'undefined []'无法转换为类型'OrderArray'。属性   类型'undefined []'中缺少'menuName'

public orderArray = [] as OrderArray;

和另一个错误

  

类型'OrderArray'不能分配给类型'string'。

this.order.orderArray = this.orderArray;

我的代码

这是 order-menu.ts

import { Order } from './../../models/order';
import { OrderArray } from './../../models/order-array';
.
.
.
public orderArray = [] as OrderArray;
.
.
.
this.order.orderArray = this.orderArray;

这是 order-menu.html

<ion-header>

  <ion-navbar color="primary">
    <ion-title>OrderMenu</ion-title>
  </ion-navbar>

</ion-header>


<ion-content padding>
  <ion-list>
    <ion-item *ngFor="let data of menuData | async ; let i = index ">
      <div item-content>
        <h2>Menu Name: {{data.payload.val().menuName}}</h2>
        <p>Price: {{data.payload.val().menuPrice}}</p>
        <ion-item>
          <ion-label>
            Jumlah Porsi:
          </ion-label>
          <ion-select [(ngModel)]="orderArray[i]">
            <ion-option value="1">1</ion-option>
            <ion-option value="2">2</ion-option>
          </ion-select>
        </ion-item>
      </div>
    </ion-item>
  </ion-list>

  <ion-item item-content>
    <ion-label floating>Catatan</ion-label>
    <ion-input type="text" [(ngModel)]="order.message"></ion-input>
  </ion-item>
  <button ion-button (click)="orderMenu()">Order</button>

</ion-content>

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

感谢@Jacopo Sciampi的答案。

代替

public orderArray = [] as OrderArray;

我应该使用

public orderArray: OrderArray[] = []

然后 代替

this.order.orderArray = this.orderArray;

我应该使用

this.order.orderArray = this.orderArray.toString();