Angular:Map error:NgFor仅支持绑定到数组等可重用项

Angular:Map error:NgFor仅支持绑定到数组等可重用项,angular,Angular,我订阅了一个obserable,以获取在角度8下运行良好的数据。当使用映射时,我需要格式化日期,我得到的错误是 Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. 所以我相信添加map操作符并更改了返回类型。我不确定我实施地图的方式有什么问题。有人能告诉我吗 export interf

我订阅了一个obserable,以获取在角度8下运行良好的数据。当使用映射时,我需要格式化日期,我得到的错误是

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
所以我相信添加map操作符并更改了返回类型。我不确定我实施地图的方式有什么问题。有人能告诉我吗

export interface IPersonNote {
    id: number;
    personId: number;
    note: string;
    authorId: number;
    authorName: string;
    fileName: string;
    mimeType: string;
    alias: string;
    createdBy: string;
    createdDate: Date;
}
原始方法

import { map } from 'rxjs/operators';
public personNotes: IPersonNote;

   loadPersonNotes() {
        this.isCurrentUser = this.id !== this.userService.getCurrentUser().id;
        this.userService.getPersonNote(this.id)
          .subscribe((x: IPersonNote) => {

            this.personNotes = x;
          });
      }
改进方法

import { map } from 'rxjs/operators';
public personNotes: IPersonNote;

loadPersonNotes() {
    this.isCurrentUser = this.id !== this.userService.getCurrentUser().id;
    this.userService.getPersonNote(this.id)
      .pipe(map(note => <any>{
        createdDate: format(note.createdDate, 'Do MMM YYYY'),
      }))
      .subscribe((x: IPersonNote) => {

        this.personNotes = x;
      });
  }
从'rxjs/operators'导入{map};
公众人物注:IPersonNote;
loadPersonNotes(){
this.isCurrentUser=this.id!==this.userService.getCurrentUser().id;
this.userService.getPersonNote(this.id)
.管道(图(注=>{
createdDate:格式(note.createdDate,“Do MMM YYYY”),
}))
.订阅((x:IPersonNote)=>{
this.personNotes=x;
});
}
用户界面


    {{note.authorName} {{note.created}} {{note.note}}
服务

public getPersonNote = (id: number): Observable<IPersonNote> =>
this.http.get<IPersonNote>(`${this.baseUrl}person-note/${id}`)
publicGetPersonNote=(id:number):可观察=>
this.http.get(`${this.baseUrl}个人注释/${id}`)

您的方法返回了错误的数据

它应该是这样的(注意
subscribe
中变量的类型):


IPersonNote
只是一个结构,不可编辑。为什么变量使用复数
pesonNotes
,而类型为单数
IPersonNote
?为什么要在结构上执行ngFor?这只是Notes的一个实例。
personNotes
是一个对象,而不是数组,因此您看到了错误…您需要在数组上使用*ngFor,而不是对象。不。我现有的方法返回了多条记录,并且显示正确。只有在映射之后,我才会得到以下错误:将personNotes声明为数组ex:
publicpersonnotes:IPersonNote[]和中的订阅
.subscribe((x:IPersonNote[])=>{
。这可以,但我如何使用映射转换日期格式。如果您在修改的代码部分看到我的帖子,考虑到我已将类型更改为数组,我如何映射以更改日期格式。当我使用映射例如.pipe(注意=>{createdDate:format(注意[0].createdDate'Do-MMM-YYYY'),})从技术上讲,我需要将记录中的每个日期字段更改为地图中的格式,而不仅仅是第一个。为了证明它有效,我注释了map语句并删除了数组符号,并且我能够使用ngForI呈现所有记录。我已经更新了帖子,以包括我的服务。
public getPersonNote = (id: number): Observable<IPersonNote> =>
this.http.get<IPersonNote>(`${this.baseUrl}person-note/${id}`)
   public personNotes: IPersonNote[]; // Not a single but few notes (or none)

   loadPersonNotes() {
        this.isCurrentUser = this.id !== this.userService.getCurrentUser().id;
        this.userService.getPersonNotesByUserId(this.id)
          .subscribe((personNotes: IPersonNote[]) => {

            this.personNotes = personNotes;
          });
   }