Angular6-JSON.stringify无法序列化循环结构

时间:2018-09-07 10:35:31

标签: typescript

我总是会收到类似标题标题的错误。这是我的类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);
  }
}

我也尝试解决此问题,但不知道如何解决。我想将家庭对象发送到数据库。这是我打字的第一步。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

  

扁平优于嵌套

我们应该保持状态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};
}