Angular - TypeError: Converting circular structure to JSON

Angular - TypeError: Converting circular structure to JSON

我在 angular 服务的映射方法中收到此错误,但我返回的数据似乎不包含任何循环引用,这是我的 json 对象:

[{"id":14,
  "sender": 
    {"id":20,"email":"p.martelliere@gmail.com","username":"test5","roles": 
    ["ROLE_USER"],"status":"active","note":"0","points":0,
    "devices":[],"job":"Caisse","showJob":false,"notificationsActivated":true,
    "connected":true,"tokenConfirm":"","birthDate":[]
},"receiver": 
     {"id":12,"email":"test2@yopmail.com","username":"test2","tokenConfirm":"",
     "job":"g\u00e9rant","showJob":false,"note":"0","points":0,
     "roles":["ROLE_USER"],"status":"active","notificationsActivated":true,
     "connected":true,"devices":[]
},
  "myNeed":"Te ster",
  "whatICanGive":"1",
  "messages":[],
  "status":"active"
}]

这是我的聊天请求 angular 实体:

export class NmChatRequest {
    // Raw attributes
    id : number;
    myNeed : string;
    whatICanGive : string;
    status : string;
    createdAt : Date;
    updatedAt : Date;
    deletedAt : Date;
    // x-to-one
    id_receiver: number;


    constructor(json? : any) {
        if (json != null) {
            this.id = json.id;
            this.myNeed = json.myNeed;
            this.whatICanGive = json.whatICanGive;
            this.status = json.status;
            this.id_receiver = json.id_receiver;
            if (json.createdAt != null) {
                this.createdAt = new Date(json.createdAt);
            }
            if (json.updatedAt != null) {
                this.updatedAt = new Date(json.updatedAt);
            }
            if (json.deletedAt != null) {
                this.deletedAt = new Date(json.deletedAt);
            }
        }
    }
}

此实体用于从 json 获取对象。

这是我的 ChatRequestService:

/**
     * Create the passed nmChatRequest.
     */
    add(nmChatRequest : NmChatRequest, token: string) : Observable<NmChatRequest> {
        let body = nmChatRequest;

        return this.http.post(this.chatUrl, body, {headers: new HttpHeaders({ 'Content-Type': 'application/json', 'X-Auth-Token': token })})
            .pipe(
                map(response => new NmChatRequest(response)),
                catchError(this.handleError)
            );
    }

我错过了什么?

感谢所有愿意花时间 read/answer 这个问题的人。

我根据上面提供的部分构建了一个 stackblitz。如果我将 json 字符串更改为对象而不是数组,它对我来说似乎工作正常。

所以没有外括号:

{"id":14,
  "sender": 
    {"id":20,"email":"p.martelliere@gmail.com","username":"test5","roles": 
    ["ROLE_USER"],"status":"active","note":"0","points":0,
    "devices":[],"job":"Caisse","showJob":false,"notificationsActivated":true,
    "connected":true,"tokenConfirm":"","birthDate":[]
},"receiver": 
     {"id":12,"email":"test2@yopmail.com","username":"test2","tokenConfirm":"",
     "job":"g\u00e9rant","showJob":false,"note":"0","points":0,
     "roles":["ROLE_USER"],"status":"active","notificationsActivated":true,
     "connected":true,"devices":[]
},
  "myNeed":"Te ster",
  "whatICanGive":"1",
  "messages":[],
  "status":"active"
}

如果您确实获得了一个数组而不是单个对象,您可能需要修改代码以传入数组的第一个元素。像这样:

map(response => new NmChatRequest(response[0]))

这里是 stackblitz:https://stackblitz.com/edit/angular-wgunjb?file=src%2Fapp%2Fapp.component.ts