角度:错误类型错误:无法读取未定义的属性___

Joh*_*n C 5 html javascript angular

即使值在浏览器中呈现,我也会收到这些错误。我不知道如何解决这个问题。

错误类型错误:无法读取未定义的属性“长度”

错误类型错误:无法读取未定义的属性“名字”

组件.ts:

teams: Team[];

ngOnInit() {
  this.getTeams();
}

constructor(private m: DataManagerService, private router: Router) { 
}

getTeams(): void {
  this.m.getTeams().subscribe(teams => this.teams = teams);
}

select(em: Team){
  this.router.navigate(['/teamView',em._id]);
}
Run Code Online (Sandbox Code Playgroud)

组件.html:

<div class="panel-body">
  <table class="table table-striped">
    <tr>
      <th>Team name</th>
      <th>Name of Team Leader</th>
    </tr>
    <tr *ngFor='let team of teams' (click)="select(team)">
      <td>{{team.TeamName}}</td>
      <td>{{team.TeamLead.FirstName}} {{team.TeamLead.LastName}}</td>
    </tr>
  </table>
  <hr>
</div>
Run Code Online (Sandbox Code Playgroud)

团队.ts:

export class Team {
    _id: string;
    TeamName: string;
    TeamLead: Employee;
    Projects:{};
    Employees: {};
}
Run Code Online (Sandbox Code Playgroud)

目的:

https://lit-coast-73093.herokuapp.com/teams

数据管理服务.ts

teams: Team[];
projects: Project[];
employees: Employee[];

constructor(private http: HttpClient) { 
}

getTeams(): Observable<Team[]> {
    return this.http.get<Team[]>(`${this.url}/teams`)
  }

getProjects(): Observable<Project[]> {
    return this.http.get<Project[]>(`${this.url}/projects`)
}

getEmployees(): Observable<Employee[]> {
    return this.http.get<Employee[]>(`${this.url}/employees`)
}
Run Code Online (Sandbox Code Playgroud)

小智 2

<tr *ngIf='teams.length > 0' *ngFor='let team of teams' (click)="select(team)">
  <td>{{team?.TeamName}}</td>
  <td>{{team?.TeamLead?.FirstName}} {{team?.TeamLead?.LastName}}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

可能性:a) Teams 对象尚未填充。因此,没有什么可以迭代的 b) 您的 API 响应不具有所有预期的属性。

像我上面建议的那样添加检查应该可以解决您的问题。LMK 如果我可以进一步解释一下。