我遇到了这些错误,而且不确定如何解决。我正在制作一个表单,该表单将向API发出发布请求。
我期望:
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>
答案 0 :(得分:0)
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
没有email
和paymentMethod
值。记录下来以确保。
答案 1 :(得分:0)
<form [formGroup]="shippingForm" (ngSubmit)="onSubmit(billingForm)">
如果我弄错了,请纠正我,但是这个billingForm
没定义。因此onSubmit
方法以undefined
作为参数被调用。
shippingForm
已定义(并且是连接到模板的那个),我猜你是说onSubmit(shippingForm)
。
答案 2 :(得分:0)
尝试在newOrder函数中使用以下代码。
createdBy:billingForm。值。电子邮件,
paymentMethod:billingForm。值 .paymentMethod,