0

Below is my class in angular 6

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

I am taking input from user and want to save that information in my db.

below is the function that save information

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

 public saveUser (api: string, model: PersonalInfo ): Observable<boolean> {
    return this._http.post<boolean>(api, {user: model});
  }

But when i try to pass PersonalInfo object to saveuser it gives me the following error.

    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)

Even i do JSON.stringify(obj). I am still getting the following error

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)

What does it mean. How can i fix this error? Thanks for the help in advance

3
  • Can you share the content of your variable model ? Commented Feb 1, 2019 at 13:02
  • it has only name, idemtity_Number, fatherName and cnic. just like the class given above Commented Feb 1, 2019 at 13:20
  • That can"t be. Just show us your model Commented Feb 1, 2019 at 13:23

2 Answers 2

1

It means that you have a model structure that is circular. For example

export class Foo {

  id: number;
  bar: Bar;
}

export class Bar {

  name: string;
  foo: Foo;
}

This kind of structure may loop infinitely for circular reference. This is the error.

I think your problem derives from parsing a JSON object into a typed data. That results into an interface instance, not in a really typed instance. So maybe you have a circular dependency.

Try investigating it with some console.log on the entire object and search for that

Sign up to request clarification or add additional context in comments.

1 Comment

ok i have done some investigation. and by removing the constructor of the personalInfo solved this problem. I dont know how but it did.
0

before making the POST request you need to serialize the JSON which you are sending i.e. JSON.stringify(object which you are planning to send)

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

public saveUser (api: string, model: PersonalInfo ): Observable<boolean> {
  return this._http.post<boolean>(api, JSON.stringify({user: model}));
}

2 Comments

i have checked this option, but still no luck
can you post what is the error that you get when you try this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.