Angular 13 http呼叫API给出错误,试图diff;测试拍卖名称One'。仅允许阵列和迭代
我有一个ASP.NET核心Web API项目,该项目由Angular 13项目召唤。我认为一切都很好,因为我可以console.log
从API发送的数据。一旦我尝试使用*ngfor
循环显示在HTML组件中,我会收到以下错误。如您所见,console.log
正在显示正确的数据。我已经在此处搜索互联网以及所有推荐的帖子,但似乎没有任何作用。任何帮助将不胜感激。
这是接口
export interface Auction {
auctionId: number;
name: string;
description: string;
longDescription: string;
bidIncrement: number;
bidAmount: number;
}
服务呼叫
//SERVICE
getAPI(): Observable<Auction[]> {
return this.http.get<Auction[]>('https://localhost:5001/api/auction')
.pipe(
map((data: Auction[]) => this.auctionList = data),
tap(data => console.log('All', JSON.stringify(data))),
catchError(this.handleError)
);
}
组件部分
export class AppComponent implements OnInit {
errorMessage = '';
auctionList: Auction[] = [];
constructor(private http: HttpClient) {}
ngOnInit(): void {
this.getAuctionList();
}
getAuctionList() {
return this.getAPI().subscribe({
next: response => {
this.auctionList = response;
},
error: err => this.errorMessage = err
});
}
}
html组件
Auction List:
<ul >
<li *ngFor="let item of auctionList" >
{{ item.auctionId }}
</li>
</ul>
I have a ASP.Net Core Web API project that is getting called by an Angular 13 project. I thought everything would be working just fine because I can console.log
the data being sent from the API. As soon as I tried to display the data in the HTML component with a *ngFor
loop I get the following error. As you can see the console.log
is showing the correct data. I've search the internet and all the recommended post here on SO but nothing has seem to work. any help would be greatly appreciated.
Here is the Interface
export interface Auction {
auctionId: number;
name: string;
description: string;
longDescription: string;
bidIncrement: number;
bidAmount: number;
}
Service call
//SERVICE
getAPI(): Observable<Auction[]> {
return this.http.get<Auction[]>('https://localhost:5001/api/auction')
.pipe(
map((data: Auction[]) => this.auctionList = data),
tap(data => console.log('All', JSON.stringify(data))),
catchError(this.handleError)
);
}
Component section
export class AppComponent implements OnInit {
errorMessage = '';
auctionList: Auction[] = [];
constructor(private http: HttpClient) {}
ngOnInit(): void {
this.getAuctionList();
}
getAuctionList() {
return this.getAPI().subscribe({
next: response => {
this.auctionList = response;
},
error: err => this.errorMessage = err
});
}
}
HTML Component
Auction List:
<ul >
<li *ngFor="let item of auctionList" >
{{ item.auctionId }}
</li>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论