Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 角度,类型脚本作为数组访问嵌套模型_Arrays_Angular_Nested_Typescript2.0 - Fatal编程技术网

Arrays 角度,类型脚本作为数组访问嵌套模型

Arrays 角度,类型脚本作为数组访问嵌套模型,arrays,angular,nested,typescript2.0,Arrays,Angular,Nested,Typescript2.0,正在尝试访问父接口的子成员(是数组)。 但是得到未定义的 export class Address { id: number; city: string; state: string; } import { Address } from './address'; export class User { id: number; name: string; alladdresses: Address[]; } 在我的组件中,声明是 users:

正在尝试访问父接口的子成员(是数组)。 但是得到未定义的

export class Address {
    id: number;
    city: string;
    state: string;
}

import { Address } from './address';
export class User {
    id: number;
    name: string;
    alladdresses: Address[];
}
在我的组件中,声明是

  users: User[] = [];
  address: Address[] = [];
我的
http.get

    selectUser(user: User) {
    let url = "http://localhost:8080/users/" + user.id
    this.http.get<User>(url).subscribe(

      res => {
        //console.log(res);
        this.selectedUser = res;
        console.log('selected user is:', this.selectedUser);

      },
      error => {
        console.log('error while getting user for ', user.id);
      },
      () => {
        console.log('complete');
      }
    );
  }
但是数组是未定义的,不是哪里出错。
您的
http.get
方法返回的数据与您期望的数据不匹配。您的api正在返回名为
地址的地址
,而您需要
所有地址

您不应该有
这个。在
控制台.log
中选择eduser.address
而不是
这个。选择eduser.alladdress
?除非您想手动实例化类实例,您应该将服务器响应类型定义为接口;现在我换了地址所有与地址混淆的地方:地址[];现在我换了地址。这帮了我的忙。好东西
createAddress() {
    let newAddress: Address = {
      id: null,
      city: "new city",
      state: "new state"
    }
    console.log(this.selectedUser)
    let address: Address[] = this.selectedUser.alladdresses
    //address.push(newAddress);
    console.log(address);
  }