Angular typeerror:将圆形结构转换为json 6

Angular typeerror:将圆形结构转换为json 6,angular,angular5,angular6,Angular,Angular5,Angular6,下面是我六年级的班级 export class PersonalInfo { private _identity_Number: string; public get identity_Number(): string { return this._identity_Number; } public set identity_Number(v: string) { this._identity_Number = v; }

下面是我六年级的班级

export class PersonalInfo {
    private _identity_Number: string;
    public get identity_Number(): string {
        return this._identity_Number;
    }
    public set identity_Number(v: string) {
        this._identity_Number = v;
    }
    private _name: string;
    public get Name(): string {
        return this._name;
    }
    public set Name(v: string) {
        this._name = v;
    }

    private _father_Name: string;
    public get father_Name(): string {
        return this._father_Name;
    }
    public set father_Name(v: string) {
        this._father_Name = v;
    }

    private _cnic: string;
    public get cnic(): string {
        return this._cnic;
    }
    public set cnic(v: string) {
        this._cnic = v;
    }
   constructor (private _services: DeoService) {}
}
我正在从用户处获取输入,并希望将该信息保存在我的数据库中

下面是保存信息的功能

this.user.saveUser(this._api, this._personalInfo);

 public saveUser (api: string, model: PersonalInfo ): Observable<boolean> {
    return this._http.post<boolean>(api, {user: model});
  }
this.user.saveUser(this.\u api,this.\u personalInfo);
public saveUser(api:string,model:PersonalInfo):可观察{
返回这个.\uhttp.post(api,{user:model});
}
但当我试图将PersonalInfo对象传递给saveuser时,它会给我以下错误

    ERROR TypeError: Converting circular structure to JSON
        at JSON.stringify (<anonymous>)
        at HttpRequest.push../node_modules/@angular/common/fesm5/http.js.HttpRequest.serializeBody (http.js:601)
        at Observable._subscribe (http.js:1460)
        at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable._trySubscribe (Observable.js:43)
        at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe (Observable.js:29)
        at subscribeTo.js:21
        at subscribeToResult (subscribeToResult.js:11)
        at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._innerSub (mergeMap.js:74)
        at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:68)
        at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:51)
ERROR-TypeError:将循环结构转换为JSON
在JSON.stringify()上
在HttpRequest.push../node_modules/@angular/common/fesm5/http.js.HttpRequest.serializeBody(http.js:601)
在可观察时订阅(http.js:1460)
在Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.\u trySubscribe(Observable.js:43)
在Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe(Observable.js:29)
在subscribeTo.js:21
在subscribeToResult(subscribeToResult.js:11)
在MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber.\u innerSub(mergeMap.js:74)
在MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber.\u tryNext(mergeMap.js:68)
在MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber.\u下一步(mergeMap.js:51)
甚至我也做JSON.stringify(obj)。我仍然得到以下错误

RROR TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at DeoService.push../src/app/shared/model/deo.service.ts.DeoService.saveUser (deo.service.ts:23)
    at ProjectComponent.push../src/app/layout/project/project.component.ts.ProjectComponent.ngOnInit (project.component.ts:59)
    at checkAndUpdateDirectiveInline (core.js:18620)
    at checkAndUpdateNodeInline (core.js:19884)
    at checkAndUpdateNode (core.js:19846)
    at debugCheckAndUpdateNode (core.js:20480)
    at debugCheckDirectivesFn (core.js:20440)
    at Object.eval [as updateDirectives] (ProjectComponent_Host.ngfactory.js? [sm]:1)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:20432)
error类型错误:将循环结构转换为JSON
在JSON.stringify()上
在DeoService.push../src/app/shared/model/deo.service.ts.DeoService.saveUser(deo.service.ts:23)
在ProjectComponent.push../src/app/layout/project/project.component.ts.ProjectComponent.ngonit(project.component.ts:59)
在checkAndUpdateDirectiveInline(core.js:18620)
在checkAndUpdateNodeLine(core.js:19884)
在checkAndUpdateNode(core.js:19846)
在debugCheckAndUpdateNode(core.js:20480)
在debugCheckDirectivesFn(core.js:20440)
在Object.eval[as updateDirectives](ProjectComponent\u Host.ngfactory.js?[sm]:1)
在Object.debugUpdateDirectives[作为updateDirectives](core.js:20432)

这是什么意思。如何修复此错误?提前感谢您的帮助

在发出POST请求之前,您需要序列化您正在发送的JSON,即
JSON.stringify(您计划发送的对象)

this.user.saveUser(this.\u api,this.\u personalInfo);
public saveUser(api:string,model:PersonalInfo):可观察{
返回这个;
}

这意味着您有一个圆形的模型结构。比如说

export class Foo {

  id: number;
  bar: Bar;
}

export class Bar {

  name: string;
  foo: Foo;
}
这种结构可以无限循环以供循环参考。这就是错误所在

我认为您的问题源于将JSON对象解析为类型化数据。这将导致接口实例,而不是真正类型化的实例。所以你可能有一个循环依赖


尝试使用一些
控制台进行调查。在整个对象上记录
并搜索该对象

我已选中此选项,但仍然没有luckcan您可以发布尝试此操作时出现的错误是什么?您可以共享变量模型的内容吗?它只有名称、idemtity\u编号、父名称和cnic。就像上面给出的类一样,这是不可能的。给我们看看你的模型好吧,我已经做了一些调查。通过删除personalInfo的构造函数,解决了这个问题。我不知道它是如何做到的。
export class Foo {

  id: number;
  bar: Bar;
}

export class Bar {

  name: string;
  foo: Foo;
}