2

I always get an error like title this comment. Here is my class CreateChildComponent:

  @Input('father') father: Father = new Father();
  child: Child = new Child();
  submitted = false;
  childList: Array<Child> = [];
  family: Family = new Family();

  constructor(private familyService: FamilyService) {
  }

  ngOnInit() {
  }

  createFamily() {
    this.family.father = new Father();
    this.family.father = this.father;
    this.father.family = this.family;
    this.family.childList = new Array<Child>();
    this.family.childList = this.childList;
    this.familyService.createFamily(cache).subscribe(data => console.log(data), error => console.log(error));
    // this.familyService.createFamily(this.family)
    //   // .subscribe(data => console.log(data), error => console.log(error));
    this.family = new Family();
  }
  addChildToList() {
    this.child.family = this.family;
    this.father.family = this.family;
    this.childList.push(this.child);
  }
}

I also tried resolve this problem but don't know how. I want to send family object to database. This is my first steps in typescript. Thanks for your help.

luksen1991
  • 85
  • 5
  • 1
    Possible duplicate of [JSON.stringify, avoid TypeError: Converting circular structure to JSON](https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json) – Roberto Zvjerković Sep 07 '18 at 10:36
  • Yes I used this code from this topic but it's not resolve my problem. – luksen1991 Sep 07 '18 at 10:40

1 Answers1

0

flat is better than nested

We should keep our state normalized

const familyMap = {
  1: new Family(),
  2: new Family(),
};

const personsMap = {
  1: new Father(),
  2: new Child(),
};

createFamily() {
  familyMap[1].father = {personId: 1};
  personsMap[1].family = {familyId: 1};
}
Buggy
  • 3,539
  • 1
  • 21
  • 38