Angular 13 http呼叫API给出错误,试图diff;测试拍卖名称One'。仅允许阵列和迭代

发布于 2025-02-05 20:04:30 字数 1569 浏览 2 评论 0原文

我有一个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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文