如何修复 Angular post 请求中的 'Cannot read property of undefined' 错误

How to fix 'Cannot read property of undefined' error in Angular post request

我遇到了这些错误,但我不确定如何解决。我正在制作一个表格,该表格将向 API.

发出 post 请求

错误类型错误:无法读取未定义的 属性 'email'

错误类型错误:无法读取未定义的 属性 'paymentMethod'

我期待:

createdBy: string from email input,
paymentMethod: string from paymentMethod select input,

但我得到的是:

createdBy: null,
paymentMethod: null

接口

export interface BillingForm {
    fullName: string;
    email: string;
    address: string;
    city: string;
    paymentMethod: string;

  }

export interface Order {
  id?: number;
  companyId: number;
  created: string;
  createdBy: string;
  paymentMethod: string;
  totalPrice: number;
  status: number;
  orderRows: OrderRows[];
}

service.ts

public postOrder(order: Order): Observable<Order> {
      console.log(order);
      return this.http.post<Order>(this.ordersURL, order, httpOptions).pipe(
        catchError(this.handleError('postOrder', order))
      );

component.ts

ngOnInit() {

    this.shippingForm = this.formBuilder.group({
      name: ['', Validators.required],
      email: ['', Validators.required],
      address: ['', Validators.required],
      paymentMethod: ['', Validators.required],
      city: ['', Validators.required],
    })
  }


  onSubmit(billingForm: BillingForm) {
    const order = this.newOrder(billingForm);

    this.productService.postOrder(order).subscribe (
      response => console.log('success', response),
      error => console.log('error', error)
    )

    this.submitted = true;

    if(this.shippingForm.invalid) {
      return;
    }
    this.success = true;

  }

  newOrder(billingForm: BillingForm ): Order {
    return {
      companyId: 6,
      created: moment().locale("sv").format("YYYY-MM-DDTLTS"),
      createdBy: billingForm.email,
      paymentMethod: billingForm.paymentMethod,
      totalPrice: this.totalPrice,
      status: 0,
      orderRows: this.orderRows

    }
  }

  get email() {
    return this.shippingForm.get("email") as FormControl;
  }

  get paymentMethod() {
    return this.shippingForm.get("paymentMethod") as FormControl;
  }   

component.html

<form [formGroup]="shippingForm" (ngSubmit)="onSubmit(billingForm)">
        <h5 *ngIf="success">Your form is valid!</h5>
          <div class="form-group col-md-6">
            <label for="inputEmail">Email</label>
            <input type="email" formControlName="email" class="form-control" id="inputEmail" placeholder="Email">
            <div *ngIf="submitted && shippingForm.controls.email.errors" class="error">
              <div *ngIf="shippingForm.controls.email.errors.required">Your email is required!</div>
            </div>
          </div>
        </div>
        <div class="form-group col-md-6">
            <label for="payment">Payment Method: </label>
            <select type="select" formControlName="paymentMethod" class="form-control" id="payment"
              aria-placeholder="Mastercard">
              <option selected> Choose...</option>
              <option *ngFor="let paymentMethod of paymentMethods">
                {{ paymentMethod }}
              </option>
            </select>
            <div *ngIf="submitted && shippingForm.controls.paymentMethod.errors" class="error">
              <div *ngIf="shippingForm.controls.paymentMethod.errors.required">Your preferred payment method is required!</div>
            </div>
          </div>
</form>
  newOrder(billingForm: BillingForm ): Order {
    return {
      companyId: 6,
      created: moment().locale("sv").format("YYYY-MM-DDTLTS"),
      createdBy: billingForm.email, // <--- HERE
      paymentMethod: billingForm.paymentMethod, // <--- HERE
      totalPrice: this.totalPrice,
      status: 0,
      orderRows: this.orderRows

    }
  }

这两行依赖于billingForm,似乎这个billingForm没有emailpaymentMethod值。登录以确保。

<form [formGroup]="shippingForm" (ngSubmit)="onSubmit(billingForm)">

如果我错了请纠正我,但是这个 billingForm 没有定义。因此 onSubmit 方法以 undefined 作为参数被调用。

shippingForm 已定义(它是连接到模板的那个),我猜你的意思是 onSubmit(shippingForm).

在 newOrder 函数中尝试使用以下代码。

createdBy: billingForm.value.email,

paymentMethod: billingForm.value.paymentMethod,