1

I need to get the list of dates from the server by using an API HTTP call. After that, I need to display the background color for that date in a calendar.

Here I'm getting data from HTTP call nicely. That data is assigning to one global variable. That global variable is getting "undefined" after assigning data(Which is getting from server).

Global variable Declaration:

unAvailableDays:any=[];

HTTP call:

beforeMonthViewRender(renderEvent: CalendarMonthViewBeforeRenderEvent): void {
    let startday =this.datepipe.transform(this.startDate,'MM-dd-yyyy');
    let endday = this.datepipe.transform(this.endDate,'MM-dd-yyyy');
    this.sharedService.GetUnAvailableTimeSlotsDatesBetweenTwoDatesByMemberID(this.sharedService.MemberID,startday,endday)
        .subscribe((data:any) => {
            this.unAvailableDays=data,
            console.log( this.unAvailableDays)
        });

    renderEvent.body.forEach(day => {
        const dayOfMonth = day.date.getDate();
        for(var i =0;i<this.unAvailableDays.length;i++) {
            console.log(dayOfMonth);
            if(new Date(this.unAvailableDays[i].date).getDate()==dayOfMonth) {
                day.cssClass = 'bg-pink';
                break;
            }
        }
    });

renderEvent is calling before data assigning to this.unAvailableDays which is mentioned inside of HTTP call.

please click here for Exption image

enter image description here

Please Help me.

Community
  • 1
  • 1
Moses Burla
  • 53
  • 1
  • 5

1 Answers1

0

You should move the renderEvent code inside the subscribe. You are trying to render something before it is assigned (render code is called most likely before your HTTP call finishes).

As a tip, always align your code properly, you will see it better.

beforeMonthViewRender(renderEvent: any): void {
  let startday = this.datepipe.transform(this.startDate, "MM-dd-yyyy");
  let endday = this.datepipe.transform(this.endDate, "MM-dd-yyyy");
  this.sharedService
    .GetUnAvailableTimeSlotsDatesBetweenTwoDatesByMemberID(
      this.sharedService.MemberID,
      startday,
      endday
    )
    .subscribe((data: any) => {
      this.unAvailableDays = data;
      console.log(this.unAvailableDays);

      renderEvent.body.forEach(day => {
        const dayOfMonth = day.date.getDate();
        for (var i = 0; i < this.unAvailableDays.length; i++) {
          console.log(dayOfMonth);
          if (new Date(this.unAvailableDays[i].date).getDate() == dayOfMonth) {
            day.cssClass = "bg-pink";
            break;
          }
        }
      });
    });
};
tudor.gergely
  • 4,800
  • 1
  • 16
  • 22