【问题标题】:Angular - TypeError: Converting circular structure to JSONAngular - TypeError:将循环结构转换为 JSON
【发布时间】:2019-01-05 00:59:12
【问题描述】:

我在 Angular 服务的 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","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"
}]

这是我的 chatRequest 角度实体:

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)
            );
    }

我错过了什么?

感谢任何愿意花时间阅读/回答这个问题的人。

【问题讨论】:

  • 能否也显示产生此错误的代码(服务方法)?
  • 是的,我刚刚编辑了我的帖子。

标签: json angular circular-reference stringify


【解决方案1】:

我从上面提供的部分构建了一个堆栈闪电战。如果我将 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]))

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

【讨论】:

  • 感谢黛博拉让我挡道。实际上是我的 nmMessage 实体构造函数引发了错误!
  • 我发送的是 TextInput 对象而不是字符串,非常感谢!
猜你喜欢
  • 2020-08-18
  • 1970-01-01
  • 2016-01-22
  • 2018-10-05
  • 2016-01-22
  • 2017-06-29
  • 1970-01-01
  • 1970-01-01
  • 2023-01-31
相关资源
最近更新 更多