错误:未捕获(承诺):类型错误:无法读取 null 的属性“状态”

时间:2021-04-23 09:45:47

标签: angular typescript

我正在尝试将票证的 JSON 结果从打开到关闭和进行中过滤,json 数据从 api 正确返回并保存在 totalTicketsDataMain 变量中。但是每当我尝试过滤数据时,存储 openTicketsData 的变量都会在 mozilla 上抛出和错误

<块引用>
ERROR Error: Uncaught (in promise): TypeError: x is null
getTicketsData/</this.openTicketsData

在谷歌浏览器上抛出错误

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'status' of null
TypeError: Cannot read property 'status' of null
    at ticketlist.component.ts:98

行号 98

this.openTicketsData = this.totalTicketsDataMain.filter(x => (x.status === "Open" || x.status === "In Progress")); //line 98

不确定我遗漏了什么或做错了什么,这是我的 TicketlistComponent

openTicketsData: Ticket[];
  totalTicketsDataMain: Ticket[];

status = "Open";
 async ngOnInit(): Promise<void> {
       await this.getTicketsData();
  }

 async getTicketsData(): Promise<void> {
    this.totalTicketsDataMain = await this.ticketService.getTickets(this.currentProjectId);
    if (this.teamId && this.eaId && this.householdId) {
      this.totalTicketsDataMain = this.totalTicketsDataMain.filter(x => x.eaId == this.eaId && x.householdId == this.householdId && x.teamId == this.teamId && x.status == this.status);
    }

    if (this.totalTicketsDataMain) {
      this.openTicketsData = this.totalTicketsDataMain.filter(x => (x.status === "Open" || x.status === "In Progress")); //line 98
      this.inProgressTicketsData = this.totalTicketsDataMain.filter(x => (x.status === "In Progress"));
      this.closedTicketsData = this.totalTicketsDataMain.filter(x => (x.status === "Closed"));
      let user =  this.userSessionService.getSession('usersession') as UserSession;
      this.assaignedToMeTicketsData = this.totalTicketsDataMain.filter(x => x.assignedTo == user.emailId);
      this.createdByMeTicketsData = this.totalTicketsDataMain.filter( x=> x.createdBy == user.emailId);
    }

    if (this.teamId == undefined  || this.eaId == undefined || this.householdId == undefined) {
      this.totalTicketsData = this.totalTicketsDataMain.filter(x => x.status == "Open" || x.status == "In Progress");
    }


  }

取票服务功能

async getTickets(projectId: string): Promise<Ticket[]> {
        try {
            const url = ServiceConstants.apiUrl + `/tickets/getticketlist?projectId=${projectId}`;
            let response = await this.httpService.get(url, { headers: this.getHeaders() }).toPromise();
            return response as Ticket[];
        } catch (error) {
            this.handleError(error);
        }
    }

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您的 if 条件的前两个不符合,您的 this.totalTicketsDataMain 将保持为空。试图获得 this.totalTicketsData 将是不可能的。此外,如果未满足所有条件,则不会将任何状态设置为打开。 在此处输入代码