Angular2承诺返回undefined

问题描述:

我有我的承诺的问题。我不知道为什么,但它确实在组件调用我的API后返回“不确定”。Angular2承诺返回undefined

送货员:

export class DeliveryMan { 
    id: number; 
    mail: string; 
    passwordToInitialize: boolean; 

    name: string; 
    administrator: boolean; 
} 

组件:

export class AdminComponent implements OnInit { 

    tabDeliveryMan = []; 

    constructor(private deliveryManService: DeliveryManService) {} 

    ngOnInit(): void { 
     this.deliveryManService.getAll() 
      .then((tabDeliveryMan) => { 
      this.tabDeliveryMan = tabDeliveryMan, 
      console.log(tabDeliveryMan) 
     }); 
    } 

    [...] 
} 

deliveryManService:

export class DeliveryManService { 
    getAll(): Promise<DeliveryMan[]> { 
     const url = this.apiUrl + 'getAllDeliveryMan'; 
     return this.http.get(url) 
     .toPromise() 
     .then((response) => response.json() as DeliveryMan[]) 
     .catch(this.handleError); 
    } 

    [...] 
} 

我得到一个日志undefined但我的回复不为空:

Response {_body: "[{"id":1,"mail":"[email protected]","password":"89c…ze":true,"name":"antoine","administrator":false}]", status: 200, ok: true, statusText: "OK", headers: Headers, …} 
+3

这可能是中投失败,'response.json()'是不可能转化为'送货员[]'。 –

+0

handleError被执行? – zxxc

+0

handleError未执行。我认为这是因为我的班级不对应。我已经添加我的送货员类来说明它 – Antoine

感谢评论我解决了我的问题。更换此:

.then((response) => response.json() as DeliveryMan[]) 

response => response.json()