0

my issue is that data i am getting is in observable so i check if data is undefined and when it is not undefined i put it into doctor object but some of my code use the data before it populate and generate error because value is undefined how can i get the value of response in a variable .

    workingDayFilter = ( d: Date ): boolean => {
    const day = d.getDay();
    let workingDays = this.doctor.workingDays;
    let result: boolean = false;

    for ( let i = 0; i < workingDays.length; i++ ) {
        if ( day == workingDays[i] ) {
            result = true;
        }
    }

    return result;

}


ngOnInit() {

    this.getDoctor();


    console.log( this.doctor );
    this.appointmentForm = this.formBuilder.group( {
        patientId: this.patientId,
        date: this.date,
        timeSlot: this.timeSlot
    } );

}
public getDoctor() {
    this.doctorService.getDoctorPublicInfo().subscribe(( data ) => {
        if ( data != undefined ) {
            this.doctor = data.json();
        }
    } );
}
ashutosh
  • 81
  • 1
  • 1
  • 9
  • when you call `workingDayFilter ` ? – Mohamed Ali RACHID Nov 07 '17 at 16:36
  • filter is get called automatically by angular material i have no control over its execution it basically add a filter to angular material datepicker . – ashutosh Nov 07 '17 at 16:38
  • I would look for that property is undefined in `workingDayFilter` before assigning the value. – dev Nov 07 '17 at 16:39
  • property is assigned at that time it is the intitalization of that property but because value is not populated in doctor object this get undefined as value – ashutosh Nov 07 '17 at 16:47

1 Answers1

0

try something like this

 ngOnInit() {

 this.getDoctor().subscribe((data) => {


    console.log(this.doctor);
    this.appointmentForm = this.formBuilder.group({
        patientId: this.patientId,
        date: this.date,
        timeSlot: this.timeSlot
    });
})

};
public getDoctor() {
 this.doctorService.getDoctorPublicInfo().map((resp) => {
    if (resp != undefined) {
        this.doctor = resp.json();
    }
});

}

NicoLA
  • 71
  • 8
  • this doesn't work angular give error DoctorMakeAppointment.html:8 ERROR Error: formGroup expects a FormGroup instance. Please pass one in i think its because angular is not getting the the formgroup instance at time . – ashutosh Nov 08 '17 at 09:51
  • I found this for the error. Is your html correct? https://stackoverflow.com/questions/38444778/formgroup-expects-a-formgroup-instance – NicoLA Nov 08 '17 at 17:52
  • my html is correct the problem is that when angular see the formgroup variable it see undefined because i moved its formbuilder code in getDoctor method you have to set the formgroup in ngOnInit method so it is ready when angular try to render the view . – ashutosh Nov 09 '17 at 10:12
  • your ans is correct in a way i can only use the doctor object in subscribe method the above code didn't work but i found another way i can not use validator in getDoctor method but i can use a filter for the datapicker and set its value in the getdoctor that seems to work for me thanks for the help. – ashutosh Nov 09 '17 at 10:16