我收到来自Web服务Doctor.service.ts数据:转换订阅对象
*getAllDoctors():Observable<Doctor[]>{
let doctor$ = this.http
.get(`${this.baseUrl}/users`,{headers: this.getHeaders()})
.map(mapDoctor)
return doctor$;}
*function mapDoctor(response:Response):Doctor[]{
return response.json().map(toDoctor);}
*function toDoctor(r:any):Doctor{
let doctor = <Doctor>({
id:r.id,
name:r.name,
username:r.username,
email:r.email,
phone:r.phone,
website:r.website,
company:r.company,
address:r.address,
});
console.log('Parsed doctor:', doctor);
return doctor;
}
在component.ts,我越来越喜欢这项资料:
doctors:Doctor[];
ngOnInit(){
this.doctorService
.getAllDoctors()
.subscribe(p => this.doctors = p);
}
问题是,我想用其他方法的医生名单,如获得医生的密钥或获取医生名单...,我已经创建了此方法:
getdoctorsById():Doctor[]{
return (this.doctors = this.doctors.filter(doctor => doctor.id === this.id));
}
但问题是,医生的名单是不确定的,它没有映射到对象医生,请我需要一些帮助!!!!!
订阅异步工作。当您调用'getdoctorsById'函数时,不保证医生的http请求已完成。 – Matt
那么,我该如何继续? –
你可以在哪里调用getdoctorsById()函数吗? – Matt