Angular-TypeError:将圆形结构转换为JSON

时间:2018-07-27 21:27:22

标签: 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","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)
            );
    }

我想念什么?

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

1 个答案:

答案 0 :(得分:1)

我从以上提供的片段中建立了一个堆叠闪电战。如果我将json字符串更改为对象而不是数组,对我来说似乎很好。

因此没有括号:

import cherrypy
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
application = get_wsgi_application()
WEBAPP = "/app-web/"
CONF_WEBAPP = {'/':
           {'tools.staticdir.on': True,
            'tools.staticdir.dir': WEBAPP,
            'tools.staticdir.index': 'index.html'}}
WEB_ROOT = '/webclient/'

class ServeWebApp(object):
    @cherrypy.expose
    def index(self):
        pass

if __name__ == '__main__':
    cherrypy.tree.graft(application, '/')
    cherrypy.tree.mount(ServeWebApp(), '/webapp', config=CONF_WEBAPP)
    cherrypy.config.update({'error_page.404': os.path.join(WEB_ROOT, "index.html")})
    cherrypy.server.socket_host = "0.0.0.0"
    cherrypy.server.socket_port = 8000
    cherrypy.server.thread_pool = 100

    cherrypy.engine.start()
    cherrypy.engine.block()

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

{"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"
}

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