gpt4 book ai didi

angular - 类型错误 : Converting circular structure to JSON property 'name' closes the circle

转载 作者:行者123 更新时间:2023-12-01 10:18:51 25 4
gpt4 key购买 nike

我在 Angular 8 中使用响应式(Reactive)表单将数据插入数据库,但我收到此错误 TypeError: Converting circular structure to JSON .据说name属性(property)是一个圆形结构,我无法找出原因。谁能帮我吗?提前致谢。

我的 component.ts

export class AddMainCategoryComponent implements OnInit {
nm;
desc;
date;

get name() {
return this.addMCform.get('name')
}

get description() {
return this.addMCform.get('description')
}
constructor(public mainCategoryService: MainCategoryService, private fb: FormBuilder) { }

addMCform = this.fb.group({
name: ['', [Validators.required, Validators.minLength(3)]],
description: ['']
})

ngOnInit() {
}

addMain_categories() {
let main_category = {
nm: this.name,
desc: this.description,
date: new Date(),

}
this.mainCategoryService.addMain_categories(main_category).then(data => {
console.log(data);
})
}
}

html

<div class="container-fluid">
<h2>Add main category</h2>
<form [formGroup]="addMCform">
<div class="form-group">
<label>Name of main category</label>
<input [class.is-invalid]="name.invalid && name.touched" formControlName="name" type="text"
class="form-control" placeholder="Enter name of category">
<div *ngIf="name.invalid && name.touched">
<small *ngIf="name.errors?.required" class="text-danger"> name is required</small>
<small *ngIf="name.errors?.minlength" class="text-danger"> name must be at least 3 characters </small>
</div>
</div>
<div class="form-group">
<label>Description:</label>
<textarea class="form-control" rows="5" input type="textarea" formControlName="description"
placeholder="Enter description"></textarea>
</div>
<button type="button" class="btn btn-primary" (click)="addMain_categories()">Add main category</button>
</form>
</div>

最佳答案

this.name返回 this.addMCform.get('name')这是一个表单控件。同样适用于 this.description .在 main_category ,您需要通过 value而不是整个表单控件。

let main_category = {
nm: this.name.value,
desc: this.description.value,
date: new Date()
};

关于angular - 类型错误 : Converting circular structure to JSON property 'name' closes the circle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57794068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com