如何以角度显示json列表对象? “尝试区分'[object Object]'时出错。仅允许数组和可迭代对象”

问题描述 投票:0回答:1

[我试图在表中显示我的json数据,但出现错误“尝试对'[object Object]'进行比较时出错。仅允许数组和可迭代对象”。我可以通过console.log看到控制台上的json对象

当我调用getAll()我的学生json列表时收到此错误

StudentComponent.html:24错误错误:尝试区分'[对象宾语]'。仅允许数组和可迭代项

-

student.ts

出口类学生{

  id: string;
  name: string;
  email: string;
  parentId: string;

}

student.component.html

<div class="panel-body">
            <table class="table table-striped">
              <thead>
              <tr>
                <th>id</th>
                <th>name</th>
                <th>email</th>
                <th>parentId</th>

              </tr>
              </thead>
              <tbody>
              <tr *ngFor="let s of student ">
                <td>{{s.id}}</td>
                <td>{{s.name}}</td>
                <td>{{s.email}}</td>
                <td>{{s.parentId}}</td>
              </tr>

              </tbody>
            </table>
          </div>

student.componen.ts

export class StudentComponent implements OnInit {

  student: Student[]=[];

  constructor(private studentService: StudentService) { }

  ngOnInit() {
      this.studentService.getAll().subscribe(res => {      this.student =  res;   console.log(res); });
  }

StudentService

export class StudentService {

  private STUDENT_PATH = '/students';

  constructor(private apiService: ApiService) {
  }


  getAll(): Observable<any> {
    return this.apiService.get(this.STUDENT_PATH).pipe(map(
      res => {
        if (res) {
          return res;
        } else {
          console.log(res);
          return {};
        }
      }
    ));
  }
json angular rest object angular8
1个回答
0
投票
{data: Array(20)}
data: Array(20)
0: {id: "b898ccd0-e0ac-4f5a-9727-3af14888fe0c", name: "Brandy Cooke", email: "[email protected]", parentId: "8d7a6e1b-9373-4d65-bb11-34420704d3bb"}
1: {id: "255067f4-fd23-4fa2-a05e-222764917563", name: "Rose Mitchell", email: "[email protected]", parentId: "3190c1c2-a7ff-4f48-87a0-84efa4ad7930"}
2: {id: "dae28df8-365d-4e88-b865-302225924aa8", name: "Gwen Gilbert", email: "[email protected]", parentId: "d6e34d2f-7e01-4d7e-b7b1-ad774e1b9911"}
3: {id: "e33a04af-2bc0-4500-a332-52a414a8e6f9", name: "Cooley Farrell", email: "[email protected]", parentId: "5ffdbbcd-e214-4a35-9d09-7d7003dcdc78"}
4: {id: "97a68161-8e21-4f0f-a2a8-e7d5c492605a", name: "Patti Ayala", email: "[email protected]", parentId: "77ce908d-eb6e-4b77-b6dd-77a2d25662f4"}
5: {id: "a0f1c667-d8f4-4d3f-9902-90588573b1b9", name: "Stacy Carpenter", email: "[email protected]", parentId: "77ce908d-eb6e-4b77-b6dd-77a2d25662f4"}
6: {id: "7e6e380d-95ce-45ee-9a88-f0152100586d", name: "Hewitt Gregory", email: "[email protected]", parentId: "a596261b-7529-4a60-a707-fa89d95b1ef9"}
7: {id: "9dcb017f-f3b0-4118-9d7f-7fa92228adfa", name: "Hobbs Ashley", email: "[email protected]", parentId: "20855ec7-900e-4a50-ba41-9885649c1dca"}
8: {id: "37d215b3-0b4f-4549-9ada-3dc6a70c3768", name: "Hollie Franks", email: "[email protected]", parentId: "5ffdbbcd-e214-4a35-9d09-7d7003dcdc78"}
9: {id: "a4ccb46b-95d9-408d-9fee-423c93462143", name: "Stuart Young", email: "[email protected]", parentId: "8d7a6e1b-9373-4d65-bb11-34420704d3bb"}
10: {id: "984b55a3-3b39-4501-8e99-b3d02b3d210b", name: "Ortega Moses", email: "[email protected]", parentId: "5ffdbbcd-e214-4a35-9d09-7d7003dcdc78"}
11: {id: "e0f215e4-6277-46e9-8dd4-8fc1b6dca060", name: "Sexton French", email: "[email protected]", parentId: "e1919ff2-b547-46ed-b111-3bf1fa26bafc"}
12: {id: "c5022024-80d9-42f3-892c-e9469ae0e37a", name: "Jackson Delacruz", email: "[email protected]", parentId: "2e8e85b9-ea70-4e95-a305-b5200d00f103"}
13: {id: "13f1a2eb-f87b-4c6a-ac4c-82c6b0a74d9e", name: "Marylou Dickerson", email: "[email protected]", parentId: "5ffdbbcd-e214-4a35-9d09-7d7003dcdc78"}
14: {id: "79bb7076-955b-4c9e-a779-330322caffa0", name: "Teri Watkins", email: "[email protected]", parentId: "20855ec7-900e-4a50-ba41-9885649c1dca"}
15: {id: "77eeed93-2c70-4e9c-8380-963e43f8a51d", name: "Robert Osborn", email: "[email protected]", parentId: "a596261b-7529-4a60-a707-fa89d95b1ef9"}
16: {id: "5836db92-e42b-4f8c-bff3-062cc846360c", name: "Levine Wynn", email: "[email protected]", parentId: "77ce908d-eb6e-4b77-b6dd-77a2d25662f4"}
17: {id: "dbd1445c-83c9-4958-886c-a9805bcf1392", name: "Maldonado Whitney", email: "[email protected]", parentId: "e1919ff2-b547-46ed-b111-3bf1fa26bafc"}
18: {id: "bed37c2b-998d-461d-ae67-ebf4f6c01866", name: "Parsons Hensley", email: "[email protected]", parentId: "3828a00d-ba5f-43b4-8ef2-f98f463b9972"}
19: {id: "1e28d4bf-56b5-46c0-8010-1d1e98b77cac", name: "Myra Reese", email: "[email protected]", parentId: "d6e34d2f-7e01-4d7e-b7b1-ad774e1b9911"}
length: 20
__proto__: Array(0)
__proto__: Object

我可以在控制台上看到它

© www.soinside.com 2019 - 2024. All rights reserved.