简体   繁体   English

为什么这个 Angular class 属性未定义?

[英]Why is this Angular class property undefined?

I am currently working on a Angular project that makes it possible to create lobbies for different web games.我目前正在开发一个 Angular 项目,该项目可以为不同的 web 游戏创建大厅。 The idea is that the application already gathers players so a web game can be started immediately.这个想法是应用程序已经聚集了玩家,因此可以立即启动 web 游戏。

But right now I am running into a problem that is caused by getting data from my Springboot Java API.但是现在我遇到了一个问题,这是由于从我的 Springboot Java API 获取数据引起的。 The Angular application gets the data correctly but when i try to convert the observable into a normal Game[] after subscribing ofcourse. Angular 应用程序正确获取数据,但是当我在订阅课程后尝试将 observable 转换为普通 Game[] 时。 It makes all the properties of the elements in the Game[] undefined.它使 Game[] 中元素的所有属性都未定义。 What is causing this to happen?是什么导致这种情况发生?

The Game Service:游戏服务:

//Returns a list of all games known to the API
  getAllGames() :Observable<Game[]>
  {
    return this.httpClient.get<Game[]>(this.connectionService.getConnectionString()+"/games",this.httpOptions)
  }

The Game class:游戏 class:

export class Game {
    public Id : String;
    public Name: String;
    public RedirectURI : String;
    public MinUserCount : Number;
    public MaxUserCount : Number;

    constructor(Id : String,Name : String,RedirectURI : String,MinUserCount : Number,MaxUserCount : Number)
    {
        this.Id = Id;
        this.Name = Name;
        this.RedirectURI = RedirectURI;
        this.MinUserCount = MinUserCount;
        this.MaxUserCount = MaxUserCount;
    }

}

The Component:组件:

 games: Game[];
 //Get all games known to the API
    this.gameService.getAllGames().subscribe( elements => this.games = elements )
 //This doesn't work all the elements of this.games are undefined.

I have also tried to work with a foreach of the array that gets returned.我还尝试使用返回的数组的 foreach 。 With no effect either也没有效果

 games: Game[];
//Get all games known to the API
    this.gameService.getAllGames().subscribe(elements => {
      elements.forEach(item => {
        var game = new Game(item.Id, item.Name, item.RedirectURI, item.MinUserCount, item.MaxUserCount)
        this.games.push(game)
      })

    })

The JSON Result for the GetRequest GetRequest 的 JSON 结果

[
    {
        "name": "QuizGame",
        "id": "dg217d65126b5e31v6d5v5d15v564d",
        "maxUserCount": 4,
        "redirectURI": "http://localhost:8082",
        "minUserCount": 1
    },
    {
        "name": "RPG",
        "id": "dt76TQWR127367125SDYATFHa32615",
        "maxUserCount": 10,
        "redirectURI": "http://localhost:8083",
        "minUserCount": 0
    },
    {
        "name": "Scribble Clone",
        "id": "378167e86dsahdgahkj312DJKHDg2d",
        "maxUserCount": 9,
        "redirectURI": "http://localhost:8084",
        "minUserCount": 1
    },
    {
        "name": "WebPonker",
        "id": "0o00dhsnvkjzbcxhgjyg23v2gh3dvg",
        "maxUserCount": 4,
        "redirectURI": "http://localhost:8085",
        "minUserCount": 4
    }
]

The properties in your JSON response start with a lowercase. JSON 响应中的属性以小写字母开头。
In your Game class, you use properties that start with an uppercase.在您的游戏 class 中,您使用以大写字母开头的属性。
I believe the parsing from JSON to a typescript object is case sensitive.我相信从 JSON 到 typescript object 的解析是区分大小写的。 Could you try to change the first letter of your properties to lowercase?您可以尝试将属性的第一个字母更改为小写吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM