无法通过Angular中的GET请求从json文件获取数据

bot_73

我需要从json GroupResult对象获取,但是那是一个新事物,并且在思考,我做错了。

我的课:

 export class GroupResult {
  Id!: number;
  Lecturer!: string;
  GroupName!: string;
  Subjects!: SubjectStatsStudent[];
}

export class SubjectStatsStudent {
  Id!: number;
  Name!: string;
  StudentResults!: StudentResult[];
}

export class StudentResult {
  Id!: number;
  Name!: string;
  Number!: number;
  LabPass!: number;
  LetionPass!: number;
  AllPass!: number;
  LabAvg!: number;
  TestAvg!: number;
  Rating!: number;
}

我试图这样做:

(service.ts)

import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import { Observable } from 'rxjs';
import { GroupResult } from '../model/group.results';

@Injectable({
    providedIn: 'root'
})

export class GroupService {
    getTest(): Observable<GroupResult> {
        return this.http.get<GroupResult>('../../assets/testGroup.json');
      }
}

(group.component.ts)

  ngOnInit(): void {
    this.groupService.getTest().subscribe((res: GroupResult) => this.Group = res);
    this.SelectedSubject = this.Group.Subjects[0];
  }

(testGroup.json)

{
    "Id": "777",
    "Lecturer": "Ivanov",
    "GroupName": "10701218",
    "Subjects": [
      {
        "Id": "5",
        "Name": "five",
        "StudentResults": [
          {
            "Id": "5",
            "Name": "five",
            "Number": "1",
            "LabPass": "1",
            "LectionPass": "1",
            "AllPass": "1",
            "LabAvg": "1",
            "TestAvg": "1",
            "Rating": "1"
          },
          {
            "Id": "3",
            "Name": "two",
            "Number": "2",
            "LabPass": "2",
            "LectionPass": "2",
            "AllPass": "2",
            "LabAvg": "2",
            "TestAvg": "2",
            "Rating": "2"
          },
          {
            "Id": "7",
            "Name": "tree",
            "Number": "3",
            "LabPass": "3",
            "LectionPass": "3",
            "AllPass": "3",
            "LabAvg": "3",
            "TestAvg": "3",
            "Rating": "3"
          }
        ]
      },

      {
        "Id": "7",
        "Name": "f77777e",
        "StudentResults": [

          {
            "Id": "5",
            "Name": "five",
            "Number": "1",
            "LabPass": "1",
            "LectionPass": "1",
            "AllPass": "1",
            "LabAvg": "1",
            "TestAvg": "1",
            "Rating": "1"
          },
          {
            "Id": "3",
            "Name": "two",
            "Number": "2",
            "LabPass": "2",
            "LectionPass": "2",
            "AllPass": "2",
            "LabAvg": "2",
            "TestAvg": "2",
            "Rating": "2"
          },
          {
            "Id": "7",
            "Name": "tree",
            "Number": "3",
            "LabPass": "3",
            "LectionPass": "3",
            "AllPass": "3",
            "LabAvg": "3",
            "TestAvg": "3",
            "Rating": "3"
          }
        ]
      },

      {
        "Id": "3",
        "Name": "fiv333e",
        "StudentResults": [

          {
            "Id": "5",
            "Name": "five",
            "Number": "1",
            "LabPass": "1",
            "LectionPass": "1",
            "AllPass": "1",
            "LabAvg": "1",
            "TestAvg": "1",
            "Rating": "1"
          },
          {
            "Id": "3",
            "Name": "two",
            "Number": "2",
            "LabPass": "2",
            "LectionPass": "2",
            "AllPass": "2",
            "LabAvg": "2",
            "TestAvg": "2",
            "Rating": "2"
          },
          {
            "Id": "7",
            "Name": "tree",
            "Number": "3",
            "LabPass": "3",
            "LectionPass": "3",
            "AllPass": "3",
            "LabAvg": "3",
            "TestAvg": "3",
            "Rating": "3"
          }
        ]
      }


    ]
}

但是我有这样的错误"Cannot read property 'Subjects' of undefined"因此,据我了解,我的组变量仍未定义/请告诉我我做错了什么?

希季吉

尝试

ngOnInit(): void {
    this.groupService.getTest().subscribe((res: GroupResult) => {
        this.Group = res;
        this.SelectedSubject = this.Group.Subjects[0];
    });
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章