将圆形结构转换为JSON

将圆形结构转换为JSON

本文介绍了Angular-TypeError:将圆形结构转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在angular service的map方法上遇到此错误,但返回的数据似乎不包含任何循环引用,这是我的json对象:

I'm getting this error at angular service's map method but my returned datas doesn't seem to contain any circular reference, here is my json object:

[{"id":14,
  "sender":
    {"id":20,"email":"[email protected]","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":"[email protected]","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角度实体:

Here's my chatRequest angular Entity:

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获取对象.

This entity is used to get the object from json.

这是我的ChatRequestService:

Here's my 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)
            );
    }

我想念什么?

感谢所有愿意花时间阅读/回答此问题的人.

Thanks to anyone who will take the time to read/answer this question.

推荐答案

我从上面提供的片段中构建了一个堆叠闪电战.如果我将json字符串更改为对象而不是数组,对我来说似乎工作正常.

I built a stackblitz from the above provided pieces. It seemed to work fine for me if I changed the json string to an object instead of an array.

因此没有括号:

{"id":14,
  "sender":
    {"id":20,"email":"[email protected]","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":"[email protected]","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"
}

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

If you are indeed getting an array and not a single object, you may need to modify your code to pass in the first element of the array. Something like this:

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

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

这篇关于Angular-TypeError:将圆形结构转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 02:48