0

I'm doing a resolver before accessing my component, like this :

 return this.unitService.getRoomsByUnitId(unitId)
        .map(rooms => {
          rooms.map((room: Room) => {
            room.beds.map(bed => {
              bed.events = new Array();
              this.patientStayService.getPatientsBedOccupancyForDateRange(unitId, startDate, endDate)
                .subscribe(patientStays => {
                  patientStays.map(patient => {
                    if (bed.id === patient.bedId) {
                      let calendarEvent: CalendarEvent = new CalendarEvent();
                      calendarEvent.patientStay = patient;
                      calendarEvent.startDate = patient.admissionPlannedDate;
                      calendarEvent.endDate = patient.earlyDischargeDate;
                      bed.events.push(calendarEvent);
                    }
                  });
                });
            });
          });
          return rooms;
        });

But in my component when I get the object "rooms", the informations about the bed and calendarEvent are not already set.

If I do

console.log(bed.events.length);

I always get 0, but If i console log my "rooms" object, I actually have events in my bed object. So I guess when I console log my events.length, the code to set events to bed is not over ?

zabop
  • 6,750
  • 3
  • 39
  • 84
Helene
  • 421
  • 3
  • 9
  • 18
  • 1
    your function made my eyes bleed. You forgot a few `return` which broke the asyncability of your resolver... ang thus, does not wait to finish before resolving. – Jscti Sep 10 '18 at 09:45
  • where should I put another return ? – Helene Sep 10 '18 at 09:49
  • https://javascript.info/promise-chaining – Jscti Sep 10 '18 at 09:55
  • sorry but can you help with an example ??... i have an observable not promise. does it work the same ? i only miss few return or should I use "then" too ? – Helene Sep 10 '18 at 12:14

0 Answers0