Angular-TypeError:将循环结构转换为JSON
我在angular service的map方法中遇到此错误,但我返回的数据似乎不包含任何循环引用,这是我的json对象:Angular-TypeError:将循环结构转换为JSON,json,angular,circular-reference,stringify,Json,Angular,Circular Reference,Stringify,我在angular service的map方法中遇到此错误,但我返回的数据似乎不包含任何循环引用,这是我的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
[{"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"
}]
这是我的聊天室请求的角度实体:
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获取对象
以下是我的聊天请求服务:
/**
* 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)
);
}
/**
*创建传递的NMCHAT请求。
*/
添加(nmChatRequest:nmChatRequest,令牌:字符串):可观察{
让body=nmchat请求;
返回this.http.post(this.chatUrl,body,{headers:newhttpheaders({'Content Type':'application/json','X-Auth-Token':Token})})
.烟斗(
map(response=>newnmchatRequest(response)),
catchError(this.handleError)
);
}
我错过了什么
感谢所有愿意花时间阅读/回答这个问题的人。我用上面提供的文章构建了一个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:你能不能也展示一下产生这个错误的代码(服务方法)?是的,我刚刚编辑了我的帖子。谢谢Deborah帮我挡道。实际上是我的nmMessage实体构造函数抛出了错误!我发送的是TextInput对象而不是字符串,非常感谢!