Core.mjs:6461错误类型错误:无法设置未定义的属性(设置‘tag’)

ddhy6vgd  于 2022-09-18  发布在  Java
关注(0)|答案(1)|浏览(161)

我陷入了这个错误,不知道我错在哪里。有谁能帮帮我?我是一个Angular 的初学者。Api只接受数组形式的标签。我尝试首先将其转换为数组,然后将其赋给createIssue对象,但它给出了这个错误。core.mjs:6461 ERROR TypeError: Cannot set properties of undefined (setting 'tags')

Create-Issue.Component.ts

export class CreateIssueComponent implements OnInit {

  projectdata!:Project[];
  createIssue!:Issue;

  constructor(private apiService:ApiService) { }

  createIssueForm = new FormGroup ({
      summary: new FormControl('',Validators.required),
      type: new FormControl(''),
      projectID: new FormControl(''),
      priority:new FormControl(''),
      description: new FormControl(''),
      assignee: new FormControl(''),
      tags:new FormControl(''),
      sprint:new FormControl(''),
      storypoint: new FormControl('')
  })

  ngOnInit(): void {
    this.getProject();
  }

  AddIssue(){
      console.log(this.createIssueForm.value);
      console.log(this.createIssueForm.value.tags.split(','));
      this.createIssue.tags = Array(this.createIssueForm.value.tags.split(','));
      console.log(this.createIssue.tags); 
      this.createIssue.status = "1";
      this.createIssue.summary = this.createIssueForm.value.summary;
      this.createIssue.priority = this.createIssueForm.value.priority;
      this.createIssue.description = this.createIssueForm.value.description;
      this.createIssue.sprint = this.createIssueForm.value.sprint;
      this.createIssue.storypoint = this.createIssueForm.value.storypoint;
      this.createIssue.type = this.createIssueForm.value.type;
      this.createIssue.assignee = this.createIssueForm.value.assignee;
      this.createIssue.projectID = this.createIssueForm.value.projectID;

      console.log(this.createIssue);
      this.apiService.createIssue(this.createIssue)
      .subscribe(
          response=>{
            console.log(response);
            alert("Issue Created");         
        });
  }

Create-Issue.model.ts

export class Issue {
    summary!:string;
    type!:string;
    projectID!:string;
    description!:string;
    priority!:string;
    assignee!:string;
    sprint!:string;
    storypoint!:string;
    status!:string;
    tags!:any;
}
k10s72fa

k10s72fa1#

将其更改为下面。属性createIssue在初始化期间为undefined,并且在初始化之前不能赋值,因此我们需要使用{}对其进行初始化。我还更改了模型,以便使用?:可以选择所有属性

export class CreateIssueComponent implements OnInit {

  projectdata!:Project[];
  createIssue!:Issue;

  constructor(private apiService:ApiService) { }

  createIssueForm = new FormGroup ({
      summary: new FormControl('',Validators.required),
      type: new FormControl(''),
      projectID: new FormControl(''),
      priority:new FormControl(''),
      description: new FormControl(''),
      assignee: new FormControl(''),
      tags:new FormControl(''),
      sprint:new FormControl(''),
      storypoint: new FormControl('')
  })

  ngOnInit(): void {
    this.getProject();
  }

  AddIssue(){
      console.log(this.createIssueForm.value);
      console.log(this.createIssueForm.value.tags.split(','));
      this.createIssue = {}; //                       <- changed here
      this.createIssue.tags = Array(this.createIssueForm.value.tags.split(','));
      console.log(this.createIssue.tags); 
      this.createIssue.status = "1";
      this.createIssue.summary = this.createIssueForm.value.summary;
      this.createIssue.priority = this.createIssueForm.value.priority;
      this.createIssue.description = this.createIssueForm.value.description;
      this.createIssue.sprint = this.createIssueForm.value.sprint;
      this.createIssue.storypoint = this.createIssueForm.value.storypoint;
      this.createIssue.type = this.createIssueForm.value.type;
      this.createIssue.assignee = this.createIssueForm.value.assignee;
      this.createIssue.projectID = this.createIssueForm.value.projectID;

      console.log(this.createIssue);
      this.apiService.createIssue(this.createIssue)
      .subscribe(
          response=>{
            console.log(response);
            alert("Issue Created");         
        });
  }

Create-Issue.model.ts

export class Issue {
    summary?:string;
    type?:string;
    projectID?:string;
    description?:string;
    priority?:string;
    assignee?:string;
    sprint?:string;
    storypoint?:string;
    status?:string;
    tags?:any;
}

相关问题