我最近使用flask创建了一个rest API,现在当我尝试将它与我的Angular Web App一起使用时,我收到了这些错误(我试图遵循官方文档中提到的相同步骤:https://angular.io/tutorial/toh-pt6)
getBills(): void {
this.BillService.getBills()
.subscribe(bills => this.billData=bills);
}
}
错误是:
错误在../src/app/Bills/add-new-bill.component.ts(61,31)中:输入' {} | BillModel []'不能分配给' BillModel []'。输入' {} | BillModel []'不能分配给' BillModel []'。输入' {}'是 不可分配给类型' BillModel []'。 财产包括'类型' {}'。
中缺少
这是我的bill.service代码:
getBills() {
return this.http.get<BillModel[]>('http://localhost:5000/bills/123')
.pipe(
catchError(this.handleError('getBills'))
);
}
这是我的账单对象的模型类:
export class BillModel {
Amount: number;
Bill_id: number;
CreatedAt: string;
From: string;
PaymentDate: string;
PaymentStatus: boolean;
To: string;
};
它完美无缺 但是我想使用可观察的机制,所以请你帮忙或向我解释为什么我会遇到这种错误
答案 0 :(得分:1)
您需要将第二个参数传递给this.handleError
方法。
getBills() {
return this.http.get<BillModel[]>('http://localhost:5000/bills/123')
.pipe(
catchError(this.handleError('getBills', []))
);
}
如果未将第二个参数传递给错误处理程序并且http observable抛出,则catchError
运算符将返回undefined
的可观察值。
private handleError<T> (operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
return of(result as T);
};
}
现在,getBills()
方法的返回类型实际上是BillModel[] | undefined
。而且您无法将其分配给bills
属性。